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


      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)

      public class MyEntity implements Serializable {
       @Id(generate = GeneratorType.AUTO)
       private Long id;
       private Boolean seen;
       // getters and setters ommitted
       public boolean equals(Object o) {
       if (o instanceof MyEntity) {
       MyEntity p = (MyEntity) o;
       if (p.getId().equals(this.getId()))
       return true;
       return false;
       return false;
       public int hashCode() {
       return this.id.hashCode();

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

      public class BusinessManagerBean implements BusinessManager, Serializable {
       EntityManager em;
       private MyEntity entity;
       // part of the remote interface
       public boolean createNew() {
       entity = new MyEntity();
       // the following change will automatically propagated to the database
       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
       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.