• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10625
  • Last Modified:

Hibernate delete cascade

I have a simple one-to-many mapping which looks the following using xdoclet in my class PersistentReportDefinition:

@hibernate.set inverse="true" table="time_span" lazy="false" name="timeSpans" cascade="all-delete-orphan"
@hibernate.collection-key column="report_def_id"
@hibernate.collection-one-to-many class="com.nexusatwork.nrs.collectable.persistence.mapped.PersistentTimeSpan"

I first create a ReportDefinition which has three such PersistentTimeSpanS in a Set. This works very well, they are saved cascading.

Later on I want to delete a time span from the set. But I do not have the original instance of PersistentReportDefinition, so I build up a new one but with only 2 PersistentTimeSpanS in the set. The primary keys are set, so hibernate knows it has to update and not to insert.

The problem is, that it does not delete the PersistentTimeSpan which is not in the Set. Does Hibernate only realize it has to remove when I actually call remove on the Set, or whats the problem?
0
stephan_zehnder
Asked:
stephan_zehnder
  • 5
  • 3
1 Solution
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

Getting the proper semantics of a parent-child relationship in Hibernate can be tricky. Have a look at Chapter 21 of the Hibernate Programming Guide and see if that doesn't point you in the right direction.
    http://www.hibernate.org/hib_docs/v3/reference/en/html/example-parentchild.html

Best regards,
Jim Cakalic
0
 
aozarovCommented:
I assume that in PersistentTimeSpan you have a reference to its parent PersistentReportDefinition, right?
When you have 'set inverse="true"'  it means that management of the DB relation is done via the other side (hence PersistentTimeSpan).
So I don't think calling clear on that set will change their relation in the database (without removing the PersistentReportDefinition from PersistentTimeSpan).
Cascade delete should remove the related PersistentTimeSpan when when you remove their parent PersistentReportDefinition (even with inverse="true").
Saving a new PersistentReportDefinition report that has 2 of the 3 PersistentTimeSpan of the other PersistentReportDefinition will not cause the removal of its third PersistentTimeSpan.
I also assume that when you created the new PersistentReportDefinition and added the 2 PersistentTimeSpan you also set their parent to be the new PersistentReportDefinition, right?
0
 
stephan_zehnderAuthor Commented:
Thanks for your answer!

> I assume that in PersistentTimeSpan you have a reference to its parent PersistentReportDefinition, right?
Yes that's right

>When you have 'set inverse="true"'  it means that management of the DB relation is done via the other side (hence PersistentTimeSpan).
>So I don't think calling clear on that set will change their relation in the database (without removing the PersistentReportDefinition from >PersistentTimeSpan).
But without a birectional association I do not have a set on the parent child.  all-delete-orphan wouldn't make sense then in my opinion.
In chapter 3.7.5 of the "Hibernate in action" book I read about all-delete-orphan the following. They have an example bidirectional relationship between item and bet, whereas bet matches my PersistentReportDefinition and bid my PersistentTimeSpan. It says:
We used cascade="all-delete-orphan" to indicate the following: ... Any persistent Bid should be deleted if it's removed from the bids collection of a persistent Item. That's excatly what I need! But doesn't work :-(

>I also assume that when you created the new PersistentReportDefinition and added the 2 PersistentTimeSpan you also set their parent to >be the new PersistentReportDefinition, right?
That's right too.

>Cascade delete should remove the related PersistentTimeSpan when when you remove their parent PersistentReportDefinition (even >with inverse="true").
Yes, that works.

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
aozarovCommented:
>> Any persistent Bid should be deleted if it's removed from the bids collection of a persistent Item. That's excatly what I need! >> But doesn't work
That should be true when you don't set inverse="true" on the collection (though when removing the parent the children will
be deleted regardless the inverse value as you saw). if you want to manipulate the children thru their parent collection then
set inverse on the collection to false.
0
 
stephan_zehnderAuthor Commented:
If I set inverse=false" hibernate doesn't delete the PersistentTimeSpan but calls an update and inserts null into the foreign key. Which gives a sql exception.
0
 
stephan_zehnderAuthor Commented:
I mean it calls update PersistentTimeSpan set report_def_id=null where id=?.
0
 
aozarovCommented:
Even when you have cascade="all-delete-orphan" ?

But for your problem:
>> Later on I want to delete a time span from the set. But I do not have the original instance of PersistentReportDefinition, so I build up a new one but >> with only 2 PersistentTimeSpanS in the set.

If you can create the two other PersistentTimeSpan then I assume you can have the key for the one PersistentTimeSpan you want to delete.
In that case why not deleteing it directly (either by a Query or using load and delete)?
0
 
stephan_zehnderAuthor Commented:
> Even when you have cascade="all-delete-orphan" ?
Yes even then.

>If you can create the two other PersistentTimeSpan then I assume you can have the key for the one PersistentTimeSpan you want to >delete. In that case why not deleteing it directly (either by a Query or using load and delete)?
This is what I'm going to do, I think. But it's a dirty solution in my opinion :-(
And I still don't understand the all-delete-orphan.
0
 
stephan_zehnderAuthor Commented:
This is what I found. It's a part of an answer from a hibernate developer to a some one:

all-delete-orphan does not work like this, it only works if you remove stuff from a collection originally loaded by hibernate.

seems like I have to do it myself! :-(
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now