一个会话bean可以调用多个实体bean 给你个例子 你仔细 研究一下 在这里也说不清促// Decompiled by DJ v2.9.9.61 Copyright 2000 Atanas Neshkov  Date: 2003/05/21 8:53:58
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   CustomerEJB.javapackage com.sun.j2ee.blueprints.customer.customer.ejb;import com.sun.j2ee.blueprints.customer.account.ejb.Account;
import com.sun.j2ee.blueprints.customer.account.ejb.AccountHome;
import com.sun.j2ee.blueprints.customer.account.exceptions.*;
import com.sun.j2ee.blueprints.customer.account.model.AccountModel;
import com.sun.j2ee.blueprints.customer.customer.exceptions.*;
import com.sun.j2ee.blueprints.customer.order.ejb.Order;
import com.sun.j2ee.blueprints.customer.order.ejb.OrderHome;
import com.sun.j2ee.blueprints.customer.order.exceptions.OrderAppException;
import com.sun.j2ee.blueprints.customer.order.model.OrderModel;
import com.sun.j2ee.blueprints.customer.util.*;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Locale;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;public class CustomerEJB
    implements SessionBean
{    public CustomerEJB()
    {
        accountHomeRef = null;
        orderHomeRef = null;
        context = null;
    }    public void changeContactInformation(ContactInformation contactinformation, String s)
        throws FinderException
    {
        try
        {
            Account account = accountHomeRef.findByPrimaryKey(s);
            account.changeContactInformation(contactinformation);
        }
        catch(FinderException finderexception)
        {
            throw new FinderException(finderexception.getMessage());
        }
        catch(RemoteException remoteexception)
        {
            throw new EJBException(remoteexception);
        }
    }    static Class _mthclass$(String s)
    {
        try
        {
            return Class.forName(s);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            throw new NoClassDefFoundError(classnotfoundexception.getMessage());
        }
    }    public boolean createAccount(String s, String s1, String s2, ContactInformation contactinformation)
        throws CreateException, DuplicateKeyException, CustomerAppException
    {
        Account account = null;
        try
        {
            if(accountHomeRef == null)
                getReferences();
            account = accountHomeRef.create(s, s1, s2, contactinformation);
        }
        catch(DuplicateKeyException _ex)
        {
            throw new DuplicateKeyException("User exits for " + s);
        }
        catch(CreateException createexception)
        {
            throw new CreateException(createexception.getMessage());
        }
        catch(AccountAppLongIdException accountapplongidexception)
        {
            throw new CustomerAppLongIdException(accountapplongidexception.getMessage());
        }
        catch(AccountAppInvalidCharException accountappinvalidcharexception)
        {
            throw new CustomerAppInvalidCharException(accountappinvalidcharexception.getMessage());
        }
        catch(AccountAppException accountappexception)
        {
            throw new CustomerAppException(accountappexception.getMessage());
        }
        catch(RemoteException remoteexception)
        {
            throw new EJBException(remoteexception);
        }
        return account != null;
    }    public int createOrder(String s, Collection collection, Address address, Address address1, String s1, String s2, String s3, 
            String s4, CreditCard creditcard, String s5, double d, Locale locale)
        throws CreateException, CustomerAppException
    {
        try
        {
            if(orderHomeRef == null)
                getReferences();
            Order order = orderHomeRef.create(collection, address, address1, s1, s2, s3, s4, creditcard, s5, s, d, locale);
            if(order == null)
                return -1;
            else
                return order.getDetails().getOrderId();
        }
        catch(CreateException createexception)
        {
            throw new CreateException(createexception.getMessage());
        }
        catch(OrderAppException orderappexception)
        {
            throw new CustomerAppException(orderappexception.getMessage());
        }
        catch(RemoteException remoteexception)
        {
            throw new EJBException(remoteexception);
        }
    }    public void ejbActivate()
    {
        getReferences();
    }    public void ejbCreate()
    {
        getReferences();
    }    public void ejbPassivate()
    {
        accountHomeRef = null;
        orderHomeRef = null;
    }    public void ejbRemove()
    {
    }    public AccountModel getAccountDetails(String s)
        throws FinderException
    {
        try
        {
            if(accountHomeRef == null)
                getReferences();
            Account account = accountHomeRef.findByPrimaryKey(s);
            return account.getDetails();
        }
        catch(FinderException finderexception)
        {
            throw new FinderException(finderexception.getMessage());
        }
        catch(RemoteException remoteexception)
        {
            throw new EJBException(remoteexception);
        }
    }    public OrderModel getOrderDetails(int i)
        throws FinderException
    {
        try
        {
            if(orderHomeRef == null)
                getReferences();
            Order order = orderHomeRef.findByPrimaryKey(new Integer(i));
            if(order == null)
                return null;
            else
                return order.getDetails();
        }
        catch(FinderException finderexception)
        {
            throw new FinderException(finderexception.getMessage());
        }
        catch(RemoteException remoteexception)
        {
            throw new EJBException(remoteexception);
        }
    }    private void getReferences()
    {
        try
        {
            InitialContext initialcontext = new InitialContext();
            if(accountHomeRef == null)
            {
                Object obj = initialcontext.lookup("java:comp/env/ejb/account/Account");
                accountHomeRef = (AccountHome)PortableRemoteObject.narrow(obj, com.sun.j2ee.blueprints.customer.account.ejb.AccountHome.class);
            }
            if(orderHomeRef == null)
            {
                Object obj1 = initialcontext.lookup("java:comp/env/ejb/order/Order");
                orderHomeRef = (OrderHome)PortableRemoteObject.narrow(obj1, com.sun.j2ee.blueprints.customer.order.ejb.OrderHome.class);
            }
        }
        catch(NamingException namingexception)
        {
            throw new EJBException("Naming Ex while getting facade references : " + namingexception);
        }
    }    public void setSessionContext(SessionContext sessioncontext)
    {
        context = sessioncontext;
    }    AccountHome accountHomeRef;
    OrderHome orderHomeRef;
    SessionContext context;
}

解决方案 »

  1.   

    其中 AccountHome 和 OrderHome都是 实体bean得本地接口
      

  2.   

    谢谢。
    只是你这个只是涉及子表的关联。
    同时仅仅是一个会话bean操作几个实体bean的例子。
    今天看了JTA好像是处理分布式事务处理的,不知道是如何处理的?
      

  3.   

    try {
    Transaction transaction = initialcontext.lookup("java:comp/Transaction");
    transaction.begin();
    调用session bean 或者 实体bean
    transaction.commit();
    } catch (Exception ex) {
    transaction.rollback();
    }
      

  4.   

    try {
    Transaction transaction = initialcontext.lookup("java:comp/Transaction");
    transaction.begin();
    entitybean1.create();
    entitybean2.create();
    entitybean2.create();
    transaction.commit();
    } catch (Exception ex) {
    transaction.rollback();
    }是不是这样实现?假如bean中有一个失败,是不是就全部失败?
      

  5.   

    是的 我忘记告诉你了 是UserTransaction 再 javax包中 你自己找一下
    try {
    UserTransaction transaction = initialcontext.lookup("java:comp/UserTransaction");
    transaction.begin();
    entitybean1.create();
    entitybean2.create();
    entitybean2.create();
    transaction.commit();
    } catch (Exception ex) {
    transaction.rollback();
    }你也可以用你的程序实现SessionSynchronization接口
    public void afterBegin() throws RemoteException {
      }
    是为了通知容器新事务的开始 可以为空public void beforeCompletion() throws RemoteException {
      }
    新事务的开始实行
    也可以为空public void afterCompletion(boolean argCompletion) throws RemoteException {if (argCompletion) {
            zaikoTensoService.commit();//举个例子
          } else {
            zaikoTensoService.rollback();
          }
      }
    表示事务结束的时候对应的操作
    argCompletion = false 的时候事务提交失败
    argCompletion = true的时候事务成功