-->
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: 1:1 association and cascade strategy for child
PostPosted: Wed Sep 21, 2005 9:02 am 
Newbie

Joined: Wed Jul 20, 2005 11:32 am
Posts: 7
Hello All,

I am trying to write a remove operation for a child object from an association. The association is on 1:0_1 type, Request being the owner and Agreement the child. Please refer to the mappings below.

Since it is mandatory for an Agreement to have a Request reference present, removal of Agreement from an association should cause the Agreement record to be deleted from the DB.

From the test data provided, when I execute the given test case, I am expecting the agreement record with DETAIL_ID=3 to get deleted from the database.

I have tried to use cascade="delete-orphan" for a one-to-one relationship. But this does work. As can be seen from the SQL at the end, the delete sql does not get generated even when the reference in the owner is set to null.

Is there any other option available? How can I achieve the cascade delete for a child in a one-to-one association.

Hibernate version: 3.0.3

Mapping documents:

Parent Object:
<hibernate-mapping>
<class table="Request" name="nl.cpl.BECParty.Bracket.Request">
<id type="java.lang.Long" column="ObjectID" name="objectID">
<generator class="increment"/>
</id>
<one-to-one name="agreement" cascade="delete-orphan" property-ref="request" class="nl.cpl.BECParty.Bracket.Agreement"/>
</class>
</hibernate-mapping>

Child Object :
<hibernate-mapping>
<class table="Agreement" name="nl.cpl.BECParty.Bracket.Agreement">
<id type="java.lang.Long" column="ObjectID" name="objectID">
<generator class="increment"/>
</id>
<many-to-one not-null="true" unique="true" column="DETAIL_ID" name="request" class="nl.cpl.BECParty.Bracket.Request"/>
</class>
</hibernate-mapping>

Test Data in the tables:
SQL> select * from request;

OBJECTID VERSION
---------- ----------
1 1
2 1
3 1
SQL> select * from Agreement ;

OBJECTID VERSION DETAIL_ID
---------- ---------- ----------
1 1 1
2 1 2
3 1 3

Code between sessionFactory.openSession() and session.close():

Request getRequest = (Request) tempSession.load(Request.class, new Long(3));
getRequest.removeAgreement();

RemoveAgreement Code:
public class Request {

private Long objectID;
private Agreement agreement;

public Request()
{
}

public Long getObjectID() {
return objectID;
}


public Agreement getAgreement() {
return agreement;
}


public void setObjectID(Long objectID) {
this.objectID = objectID;
}

public void setAgreement(Agreement agreement) {
this.agreement = agreement;
}


public void removeAgreement() {
setAgreement(null);
}

}

Name and version of the database you are using:
Oracle 10g

The generated SQL (show_sql=true):
Hibernate: select request0_.ObjectID as ObjectID1_, request0_.VERSION as VERSION0_1_, agreement1_.ObjectID as ObjectID0_, agreement1_.VERSION as VERSION1_0_, agreement1_.DETAIL_ID as DETAIL3_1_0_ from Request request0_ left outer join Agreement agreement1_ on request0_.ObjectID=agreement1_.DETAIL_ID where request0_.ObjectID=?


regards,
~Vinayak


Top
 Profile  
 
 Post subject: Re: 1:1 association and cascade strategy for child
PostPosted: Fri Sep 23, 2005 1:14 am 
Newbie

Joined: Wed Jul 20, 2005 11:32 am
Posts: 7
Hello Everyone,

Please let me know if it is possible to implement cascade delete using hibernate in a -

1:1 association (Mandatory)

where the removal of association causes the child to be physically deleted from the database.

regards,
~Vinayak


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 11, 2005 4:14 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 5:09 am
Posts: 22
Hi,

I'm having the same problem as you do. It seems that mapping the assocation 'in reverse' (the many-to-one and one-to-one switched) works, but that causes the foreign key to be placed in the wrong (i.e., not the one I want) table.
I would expect the one-to-one to work like <set inverse=true"> when used like this, but it doesn't. The documentation doesn't say anything in particular about cascade semantics on a bi-directional one-to-one with a unique foreign key.

Can anyone shed some light on this? Is using <one-to-one> on the parent side of the relation supported with a cascade="all-delete-orphan", or should the parent always be mapped as <many-to-one unique="true">? This last case causes the parent table to have a foreign key to the child table, while I'd rather have this reversed.

Joris


Top
 Profile  
 
 Post subject: delete-orphan isn't supported for XXX-to-one associations
PostPosted: Thu Jun 15, 2006 8:25 pm 
Newbie

Joined: Thu Jun 15, 2006 7:32 pm
Posts: 3
Unfortunately, "delete-orphan" is meaningless for one-to-one associations; it's only supported for one-to-many relationships. See:

http://www.hibernate.org/hib_docs/v3/re ... transitive

I'm frustrated by this, too. My current work-around is to turn the one-to-one into a one-to-many, modelling the property as a Set within my domain object.

This is messy, though: I'd rather Hibernate supported a cleaner way to persist 1-to{0,1} relationships.


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.