• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 JPA & Hibernate One-to-Many leaves orphan children?

 
Stu Quinn
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,

I have seen a few postings that touched on this topic but none that addressed this particular issue. I have what I believe to be a very straight-forward parent child relationship. It is not bi-directional. When the child is removed from the collection of the parent object and the parent object is merged the objects look just fine, however when I check my tables I see my child table still has the row present for the child, however the join column back to the parent is null. I had expected that the row would be deleted. I must be missing something obvious and would ask your kind assistance.

I am working over Oracle 10g, JBOSS 4.2.2 and Hibernate 3.2.4.sp1




Thank you again for any help.

Stu
 
Tomasz Szymanski
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's because in your Parent class you have configured the children collection with cascade="all". "all" is insufficient.
Take a look: https://www.hibernate.org/116.html#A18.
 
Stu Quinn
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz,

Thank you, however with the JPA annotations it appears as though .ALL should be sufficient. There is no delete-orphan option. Below is an excerpt from Chapter 3 of the Hibernate Working With Objects reference Hibernate Reference


For each basic operation of the entity manager - including persist(), merge(), remove(), refresh() - there is a corresponding cascade style. Respectively, the cascade styles are named PERSIST, MERGE, REMOVE, REFRESH. If you want an operation to be cascaded to associated entity (or collection of entities), you must indicate that in the association annotation:

@OneToOne(cascade=CascadeType.PERSIST)

Cascading options can be combined:

@OneToOne(cascade= { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.REFRESH } )

You may even use CascadeType.ALL to specify that all operations should be cascaded for a particular association. Remember that by default, no operation is cascaded.



Of course, this being said it should work, but does not work so I appreciate your input.

We will see where this leads

Thanks,
Stu
 
Tomasz Szymanski
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see, I thought you were using Hibernate annotations, not JPA.

The way I see it:
The meaning of "all" cascading type in JPA is the same as in Hibernate, which you quoted, ie. the child will be deleted only if you delete the parent. You perform a "remove" operation on the parent and it is cascaded to the child (all the children, to be precise).
Since you're not deleting the parent, just diconnecting one of the children, you're not performing any key operation on the parent - there's nothing to cascade in fact. Automatically deleting a child in that case is an extra feature, provided by Hibernate (by "delete-orphan") but... not included in the JPA API.
It seems you can't get it done easy way if you don't want to use Hibernate specific annotations.

I've found a short topic about it: http://www.coderanch.com/t/218171/Object-Relational-Mapping/java/all-delete-orphan-JPA
 
Stu Quinn
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomasz,

That is exactly the issue. Thank you for your help.

Stu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic