1 Reply Latest reply on Feb 8, 2006 7:10 PM by Alexander Langer

    Entity Beans within stateful session beans and em.refresh()

    Alexander Langer Newbie

      Hi!

      I quite don't understand why I have to call em.refresh(entitybean) in every funtion of a stateful session bean that trys to modify bean values. Maybe someone can give me a pointer. This is my code (non-important columsn snipped)

      Entity(access=AccessType.FIELD)
      @Table(name="myentity")
      public class MyEntity implements Serializable {
      
       @Id(generate = GeneratorType.AUTO)
       private Long id;
      
       @Column
       private Boolean seen;
      
       // getters and setters ommitted
      
       @Override
       public boolean equals(Object o) {
       if (o instanceof MyEntity) {
       MyEntity p = (MyEntity) o;
       if (p.getId().equals(this.getId()))
       return true;
       else
       return false;
       }
       return false;
       }
      
       @Override
       public int hashCode() {
       return this.id.hashCode();
       }
      
      }
      


      The stateful session bean is saving a local copy of the used MyEntity in its state:

      @Stateful
      @Remote
      @SecurityDomain("other")
      public class BusinessManagerBean implements BusinessManager, Serializable {
      
       @PersistenceContext(unitName="mypersistencecontext")
       EntityManager em;
      
       private MyEntity entity;
      
       // part of the remote interface
       public boolean createNew() {
       entity = new MyEntity();
       entity.setValue(true);
       em.persist(entity);
      
       // the following change will automatically propagated to the database
       entity.setValue(false);
       return true;
       }
      
       // public remote interface function
       public boolean doBusinessLogic() {
       // em.refresh(entity);
       // change not propagatec to database unless the above em.refresh() is commented out
       entity.setValue(true);
       return true;
       }
      }
      
      


      The entity is not detached from the database (e.g. em.remove(entity) works flawlessly). However, I have no idea why I have to em.refresh() it each time.

      (I tried adding em.flush() at various places with no success).

      The above is only a small testcase of mine that still shows this behaviour. In reality, the MyEntity has a HUGE datagraph within ManyToMany/OneToMany relationships, and calling em.refresh() each time takes a copule of seconds, so as you can image it's inacceptible for me to call em.refresh() in every singe business function.

      Thanks for any pointers.

      Alex