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.  [ 2 posts ] 
Author Message
 Post subject: delete one-to-many
PostPosted: Mon Jul 23, 2007 1:57 am 
Newbie

Joined: Thu Feb 01, 2007 1:57 am
Posts: 17
Location: Mumbai
HI Friends,

I am using one-to-many relationship,
I have Tow table table1 and table2 and table data is as,

table1

ID LAST FIRST AGE
1 Kumar Nagendra 26
2 Rao Komal 24

table2
ID parent_ID test result
1 1 blood A+
2 1 Pulse 75
===============================================
And I want to delete all data of parent_ID 1 from table2 and my code is,
=========================================
Transaction transaction = null;
Session session = HibernateUtil.currentSession() ;
transaction = session.beginTransaction();
TABLE1 table1 = (TABLE1)session.load(TABLE1.class,1);

Set l = table1.getStories();

try
{
Iterator i = l.iterator();
while(i.hasNext())
{
i.next();
i.remove();
}
// table1.setStories(l);
//session.save(table1);
transaction.commit();
}
=============================================
But is don’t effect on database. This is wrong way or I am missing something plz help me.
==============================================
And HBM file is as
<hibernate-mapping>
<class name="TABLE1" table="table1">
<id name="id" unsaved-value="0" >
<generator class="increment" />
</id>
<set name="stories" cascade="all-delete-orphan" inverse="true">
<key column="parent_id"/>
<one-to-many class="TABLE2"/>
</set>
<property name="last" type="string"/>
<property name="first" type="string"/>
<property name="age" type="string"/>
</class>

<class name="TABLE2" table="table2">
<id name="id" unsaved-value="0">
<generator class="increment"/>
</id>
<property name="test" type="string"/>
<property name="results" type="string"/>
<many-to-one name="parent" column="parent_id" not-null="true"/>
</class>
</hibernate-mapping>



Thanks in adv


Top
 Profile  
 
 Post subject: RE: delete one-to-many
PostPosted: Tue Jul 24, 2007 12:12 am 
Newbie

Joined: Sat Oct 28, 2006 6:16 am
Posts: 17
I think the problem is that you are updating the inverse end of a bi-directional association. The hibernate documentation says:

Quote:
Changes made only to the inverse end of the association are not persisted


This is at http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#collections-bidirectional

In a bi-directional one-to-many association the inverse end must be the "many" end. So to get the desired result you have to update the "one" end of the association. The following code does this:

Code:
Transaction transaction = null;
Session session = HibernateUtil.currentSession() ;
transaction = session.beginTransaction();
TABLE1 table1 = (TABLE1)session.load(TABLE1.class,1);

Set l = table1.getStories();

try
{
Iterator i = l.iterator();
while(i.hasNext())
{
((TABLE2)i.next()).setParent(null);
}
// table1.setStories(l);
//session.save(table1);
transaction.commit();
}


In the code above we are updating the "one" end of the bi-directional association by setting TABLE2.parent to null.

The hibernate documentation also says:

Quote:
Mapping one end of an association with inverse="true" doesn't affect the operation of cascades, these are orthogonal concepts!


So I expect that the associated TABLE2 objects will be deleted because of the cascade option for TABLE1.stories is set to all-delete-orphan.

I hope that helps (and works!).

Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.