It's not a secret anymore!
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Delete/update associations question" Watch "Delete/update associations question" New topic

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: 15302

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

Joined: Jul 20, 2005
Posts: 10441

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:
subject: Delete/update associations question
It's not a secret anymore!