1

Collection.clear() will delete children whenever Session flushes.

What about OneToOne? Setting to null is not a correct way. No clear() method for OneToOne exists.

Table_A has a column (not its PK!) that OneToOne references Table_B.

I wonder if Hib works as expected in my case?

My Cascading options are set to "all,delete-orphan".

Table_A @OneToOne Table_B

Table_B @OneToMany Table_C

Now it looks like Table_A . getTable_B . getTable_C_Collection()

Suppose there are elements in Table_C collection.

What I expect from Hibernate: if I set Table_B link to null, then

all Table_C collection elements MUST BE DELETED.

It does not happen. They become ORPHANED !

EugeneP
  • 11,783
  • 32
  • 96
  • 142
  • Can you post your hibernate config (or the annotations if using Hibernate Annotations)? It sounds like you might have a configuration problem. – Jim Hurne Apr 18 '10 at 08:53
  • Does deleting Table_B directly delete the members in Table_C, or is this only an issue when you set the Table_B link to null in Table_A? – Jim Hurne Apr 18 '10 at 09:08
  • @Jim Hurne To your second comment: if I understand you, then my answer is this: I do not want to delete an instance of B! I want to deal with A, it is the main object, it has references to B, so I simply want to delete his B link and expect that Hibernate will do other stuff. – EugeneP Apr 18 '10 at 09:47

3 Answers3

2

Have you tried "all-delete-orphan" instead of "all,delete-orphan"? The two are supposed to work the same, but there have been problems in the past where one has different behaviour from the other. Such issues are actually bugs, so if using one rather than the other does behave differently, be sure to open a bug.

You might also want to play with setting inverse='true' in your Hibernate mappings. From the documentation, and from this Stack Overflow Question, it looks like inverse=true is often the key to getting cascade deletes to work properly.

Community
  • 1
  • 1
Jim Hurne
  • 7,187
  • 4
  • 44
  • 44
1

No, deleting parent will delete children if you have cascading turned on.

You need to send DELETE SQL to the database, and merely setting a reference to null won't do that.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • @duffymo But that's weird. You see: how we delete linked objects (children): Collection.clear(). And how to do this thing with OneToOne? I also need something like OneToOneLink.clear() and the only thing to do this as I see it is set reference to NULL. – EugeneP Apr 18 '10 at 09:49
1

Hibernate's support for cascading actions can be a bit quirky at times. If you find that you cannot get the desired behavior out-of-the-box, you can implement a Hibernate Interceptor or a Hibernate event listener. It's actually pretty easy to do ether, and the effort is worth it (your main code remains clean).

In this case, you probably want to implement a PostUpdateEventListener, or a PreUpdateEventListener.

Jim Hurne
  • 7,187
  • 4
  • 44
  • 44