-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Delete orphan record with unidirectional one-to-one
PostPosted: Wed Feb 01, 2006 6:41 pm 
Newbie

Joined: Mon Apr 04, 2005 6:05 pm
Posts: 4
Location: Toronto, Canada
I can't get Hibernate to perform deletion of an orphan record in my unidirectional one-to-one association.

I have a test case that:
- gets a disconnected "Parent" object
- nullifies the "child" attribute
- updates the "Parent".

After which the Parent FK to Child is null, but the orphaned record still exists in the Child table.

This would work if it was a collection of Child objects and I was removing it because the Hibernate Collection would store metadata regarding which records to orphan. However in this case I need an optional singular Child whose lifecycle is tied to the Parent.

In this case, is there a way to accomplish the orphan record deletion without having to manually delete the Child myself?

Hibernate 3.0.5

Mapping documents of "Parent":
<many-to-one name="child"
class="Child"
cascade="all-delete-orphan"
column="child_id"
lazy="false"
unique="true"
not-null="false"
/>

An implementation of a unidirectional one-to-one association on a foreign key.
http://www.hibernate.org/hib_docs/v3/re ... tional-121


Top
 Profile  
 
 Post subject: RE: Delete orphan record with unidirectional one-to-one
PostPosted: Thu Feb 02, 2006 4:59 am 
Newbie

Joined: Mon Nov 14, 2005 6:40 am
Posts: 4
Location: Duesseldorf/Germany
Hi,

I had a similar problem and solved it by using a set instead of a <one-to-one>- or <many-to-one unique="true">-mapping.

This is the mapping of the parent class:

<set
name="children"
cascade="all,delete-orphan"
inverse="true">
<meta attribute="scope-set">private</meta>
<meta attribute="scope-get">private</meta>
<key column="FKPARENT"/>
<one-to-many class="Child"/>
</set>

This will generate private getter and setter for children.
Since I want a one-two-one-relatonship, I added two convinience methods:

public void setChild(Child child){
if (getChildren() == null){
setChildren(new HashSet());
}
getChildren().clear();
if (child != null) {
getChildren().add(child);
child.setParent(this);
}
}

public Child getChild(){
if (getChildren() != null & !getChildren().isEmpty()){
return (Child) getChildren().toArray()[0];
} else {
return null;
}
}

Please note that delete-orphan only works with the Hibernate-specific implementation of a Set (somthing like 'PersistentSet'). That means that it only works if your parent is assigned to a HibernateSession.


I used a bidiretional relationship, but that should not cause any problems:

Mapping of Child:

<many-to-one
name="parent"
class="Parent"
cascade="none">
<column
name="FKPARENT"
not-null="true"
unique="true"/>
</many-to-one>



HTH,
Kodicic


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 3:55 pm 
Newbie

Joined: Mon Apr 04, 2005 6:05 pm
Posts: 4
Location: Toronto, Canada
Thanks kodicic, the possible solution of using a Collection and only storing 0 or 1 Children in it had occured to me. The public interface could remain the same and I'd just have to add some private Collection accessor method for Hibernate's use.

However, for my situation, that would mean flipping the FK from the Parent table to the Child table.

It looks like the original, somewhat arbitrary selection of where to put the FK of the 1-to-1 relationship may be forcing me to change the schema. In a 1-to-1 Parent-Child relationship it does seems more natural to put the FK in the Parent's table, especially if you want to be able to express it's optionality.

My original question still stands though, if I assume the schema is frozen with the FK on the Parent's side, can Hibernate cascade the orphan deletion in a 1-to-1 parent child relationship?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 5:08 am 
Newbie

Joined: Mon Nov 14, 2005 6:40 am
Posts: 4
Location: Duesseldorf/Germany
Quote:
It looks like the original, somewhat arbitrary selection of where to put the FK of the 1-to-1 relationship may be forcing me to change the schema. In a 1-to-1 Parent-Child relationship it does seems more natural to put the FK in the Parent's table, especially if you want to be able to express it's optionality.


We put the FK into the child table to have the ability to change the 1:1- relationship into a 1:n-relationship in the future without changing the schema.

But I agree, if you are sure that it will remain a 1:1-relationship, it is sensible to put the FK into the parent table.


Quote:
My original question still stands though, if I assume the schema is frozen with the FK on the Parent's side, can Hibernate cascade the orphan deletion in a 1-to-1 parent child relationship?


Your mapping seems to be ok to me and I cannot see any reason why delete-orphan should not work. The only idea I have is to establish a cascaded delete on database level :-(

Or you could set 'unique' to 'false' and add the same getter/setter as mentioned in my first reply.

Regards,
Kodicic


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.