permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes Delete/update associations question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Delete/update associations question" Watch "Delete/update associations question" New topic
Author

Delete/update associations question

Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
I have mapped User --> UserProduct association(one-to-many) in hibernate as follows:


I also have User class and UserProduct classes as follows:


Here is the part of User.hbm.xml that shows the association:


Here is the UserProduct.hbm.xml file.


How do I delete all userProducts for a given User ?

When I load a user in the Hibernate session and do the following:


The user records do not get deleted !

Can someone show me the correct way to modify hibernate associations.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I think I'll shimmy this on over to our ORM forum where folk talk about Hibernate on a regular basis.


GenRocket - Experts at Building Test Data
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9910
    
158

Change your User hbm to contain:



And the code:



Also, have a look at the "Cascading lifecycle" section at Hibernate reference - Parent child example


[My Blog] [JavaRanch Journal]
Naveen Sampra
Ranch Hand

Joined: Jun 11, 2003
Posts: 70
Thank you very much for the reply and the useful link.

You are precisely correct. I was missing cascade="all-delete-orphan". That fixed the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete/update associations question
 
Similar Threads
Hibernate Association
Hibernate Association
Ternary Association
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.
Hibernate, User and Address with association table between