1 Reply Latest reply on Aug 31, 2007 9:18 AM by Matthias Huber

    @EntityListeners with seam

    Matthias Huber Newbie

      Hi ,

      Is there a seam-way to implement @Entitylisteners ? I've read in the book 'Java Persistence with Hibernate' that the user of EntityManagers is not allowed in callbacks. However; thats the usecase:

      I've two Entities which are associated through a @ManyToMany association and i like to implement a last-save-wins-strategy for this set.

      @Entity
      @Table (name="t_foo")
      @EntityListeners(value={Foolistener.class})
      public class Foo {
       @Id
       private Long id;
      
       @Basic
       private String name;
      
       @ManyToMany (fetch=FetchType.LAZY)
       @JoinTable (name="t_foo_bar",
       joinColumns=@JoinColumn(name="foo_id"),
       inverseJoinColumns=@JoinColumn(name="bar_id"))
       private Set<Bar> bars;
      
      .....
      
      }
      


      @Entity
      @Table (name="t_bar")
      public class Bar {
       @Id
       private Long id;
       @Basic
       private String name;
       @ManyToMany (fetch=FetchType.LAZY, mappedBy="bars")
       private Set<Foo> foos;
      
      ....
      }
      


      public class Foolistener {
      
       @PrePersist
       public void prePersist (Foo foo) {
       // delete the all entries in t_foo_bar which
       // reference too foo.getId()
       }
      
       @PreUpdate
       public void preUpdate (Foo foo) {
       // delete the all entries in t_foo_bar which
       // reference too foo.getId()
       }
      
      }
      


      The first issue on this is, that the callback is only called if the Entity-fields are changed, but not, if the included set changed only. Does anyone know why?
      I know i could add a 'changed-set' - flag .. which is not very comfortable if the set is changed directly ;)

      I tried to add @Name and @In annotations to the FooListener - but this seams not to work.

      I've not much experience with Hibernate-jpa because i worked with jpox-jdo before. If i remember correctly it was no problem to have (restricted) database-access in callback-methods .

      Any ideas ?