-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: preUpdate not called when collection changed
PostPosted: Mon Apr 28, 2008 8:51 am 
Newbie

Joined: Mon Apr 28, 2008 8:25 am
Posts: 2
Hibernate version: 3.2 (annotations 3.3.1, entitymanager 3.3.2)
Name and version of the database you are using: HSQLDB 1.8.0.9

I have 2 classes (Computer and Software) with a ManyToMany association. Since the access is always done from the Computer side, the Software Entity has no coresponding ManyToMany field.

Any change made to a Computer should be logged (with the preUpdate method).

Code:
@Entity
public class Computer {

   @Id @Generated
   private long id;   
   private String name;
   @ManyToMany
   private Collection<Software> software;

   getters and setters...

   @PreUpdate
   public void preUpdate{
      System.out.println("Computer updated");
   }
}

@Entity
public class Software {

   @Id @Generated
   private long id;
   private String name;

   getters and setters...
}


When I change the name of a Computer, the preUpdate is called as it should, but when I add a Software to a Computer's software Collection, the preUpdate method is NOT called.

Is this behaviour normal or am I doing something wrong? How can I make the preUpdate method beeing called when a collection is changed?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 28, 2008 3:29 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

If I am not mistaken @PreUpdate is called before db update operations. Adding sofware would trigger an insert operations though. Try also adding a @prePersist. Also check http://www.hibernate.org/hib_docs/entitymanager/reference/en/html_single/#listeners.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 4:43 am 
Newbie

Joined: Mon Apr 28, 2008 8:25 am
Posts: 2
Thanks for the hint Hardy.

When I add a Software the first time, @prePersist is called on the Software entity, but not on the Computer. Any subsequent adding or removing of software to or from Computers (remember: it's a many-to-many association) does neither trigger any entity lifecycle methods on the Computer or the Software.

As a workaround, I can add a mapping class with two many-to-one and the coresponding one-to-many associations on the Computer and Software.

In general, it seems to me that the entity lifecycle methods are only called when:
- a field on a database table changes
AND
- there is a entity for that table
AND
- the entity has a field that maps to the changed field on the database

can anyone approve this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.