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() } }