EJB 3.0中的merge被称为更新,通过merge (person)方法可以把Entity Bean更新到数据库。那么该方法跟无状态Bean和有状态Bean有什么关系吗?它跟transaction-scoped persistence context和extended persistence context上下文有没有关系?
在有状态Bean,且为extended persistence context上下文中,好像并不需要使用merge方法。

解决方案 »

  1.   

    merge()方法是EntityManager的方法 跟无状态Bean和有状态Bean没有任何关系
    transaction-scoped persistence context是由容器创建EntityManager,随着调用session bean方法的结束 事务被提交,extended persistence context面向于stateful session bean的它可以跨越多个Transaction,它随着stateful session bean的销毁EntityManager才销毁!
      

  2.   

    当实体Bean传到客户端时,再传回容器需要调用merge,这个客户端应该怎么理解?如果这个客户端是与实体Bean同在一个EJB容器中呢?又或者这个客户端是工作于某台PC中的一个J2SE程序?又或这个客户端是工作于JSP中?
      

  3.   


    如果我在一个无状态会话Bean中使用一个transaction-scoped persistence context的EntityManager,比方说,这个会话Bean有几个改变实体Bean的方法,person是个实体Bean实例
    如:
    private static Person person;
    public void updateName(string name){
    person.setName(name);
    }
    public void updateID(string ID){
    person.setID(ID);
    }
    public void updateSex(string Sex){
    person.setSex(Sex);
    }
    是不是都要在每个update方法中都要调用EntityManager.persist(person);
      

  4.   


    看是否在一个JVM上,如果是在同一个JVM上,容器返回的不是一个实体Bean的代理,如果不在一个JVM上,那么容器返回的是一个代理对象,每调用一次实体Bean的方法都是一个RMI的接口交互,所以客户端要减少对实体Bean方法的调用!!
      

  5.   


    你的session bean设计的不正确 太细了 应该是粗粒度设计 减少rmi交互
    例如
    public void update(String id, String name, String gender) {
        Person person =  entityManager.find(Person.class, id);
         if(null != name && !"".equals(name.trim()) {
            person.setName(name);
         } 
        ........
        
        entityManager.merge(person);}
      

  6.   


    只有刚刚new一个新的实体Bean后要调用EntityManager.persist(person); 吧,以后更改了这个实体Bean是调用entityManager.merge(person); 
      

  7.   

    十分感谢woming66,您太热心了。