[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - @Entitylisteners

motte1979 do-not-reply at jboss.com
Fri Aug 31 09:17:36 EDT 2007


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 seems 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 ?

(see also http://www.jboss.com/index.html?module=bb&op=viewtopic&t=117557[/url])

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4079991#4079991

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4079991



More information about the jboss-user mailing list