-->
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.  [ 5 posts ] 
Author Message
 Post subject: Delete object with 2 one-to-many relationships
PostPosted: Mon Apr 14, 2008 9:19 am 
Newbie

Joined: Sun Feb 10, 2008 3:28 pm
Posts: 3
Hi, I am using Hibernate Core 3.2.6ga.

I have a class named Subgroup with 2 one-to-many relationships. Here's my mapping (only the excerpt for the relationships):

<hibernate-mapping package="domain">
<class name="Subgroup" table="subgroups" schema="lsf">

<set name="fractionHistory" inverse="true" cascade="all,delete-orphan"
lazy="true">
<key column="subgroupId" />
<one-to-many class="Fraction" />
</set>

<set name="members" inverse="true" cascade="all,delete-orphan"
lazy="true">
<key column="subgroupId" />
<one-to-many class="SubgroupMember" />
</set>
</class>
</hibernate-mapping>


In my Java code, I am deleting a Subgroup object, loaded from a previous Session, and deleting it in a new Session, this way:

Code:
         Transaction tx = session.beginTransaction();
                 
         session.delete(subgroup);
         
         tx.commit();


It works fine, without any exceptions. The problem is that it loads both sets, then deletes them and finally deletes the subgroup. How can I change my mapping file and/or Java code in a way that makes Hibernate send only delete statements to the DB (Oracle 10g) without fetching the entire associations?

The generated SQL code (DEBUG level) :

[14 Apr 2008 14:42:11]
/* load one-to-many domain.Subgroup.fractionHistory */ select
fractionhi0_.subgroupId as subgroupId1_,
fractionhi0_.id as id1_,
fractionhi0_.id as id4_0_,
fractionhi0_.value as value4_0_,
fractionhi0_.ccomment as ccomment4_0_,
fractionhi0_.modifiedAt as modifiedAt4_0_,
fractionhi0_.modifiedBy as modifiedBy4_0_,
fractionhi0_.subgroupId as subgroupId4_0_,
( SELECT
sgrp.name
FROM
lsf.subgroups sgrp
WHERE
sgrp.id = fractionhi0_.subgroupId ) as formula10_0_,
( SELECT
grp.name
FROM
lsf.subgroups sgrp
INNER JOIN
lsf.groups grp
ON sgrp.groupId = grp.id
WHERE
sgrp.id = fractionhi0_.subgroupId ) as formula11_0_
from
lsf.fractions fractionhi0_
where
fractionhi0_.subgroupId=?
[14 Apr 2008 14:42:11] binding '287' to parameter: 1
[14 Apr 2008 14:42:11] returning '429' as column: id4_0_
[14 Apr 2008 14:42:11] returning '0' as column: value4_0_
[14 Apr 2008 14:42:11] returning 'Initial value' as column: ccomment4_0_
[14 Apr 2008 14:42:11] returning '2008-04-14 14:11:01' as column: modifiedAt4_0_
[14 Apr 2008 14:42:11] returning 'fdmanana' as column: modifiedBy4_0_
[14 Apr 2008 14:42:11] returning '287' as column: subgroupId4_0_
[14 Apr 2008 14:42:11] returning '287' as column: subgroupId4_0_
[14 Apr 2008 14:42:11] returning 'AA_TEST_SG' as column: formula10_0_
[14 Apr 2008 14:42:11] returning 'test_group' as column: formula11_0_
[14 Apr 2008 14:42:11] returning '287' as column: subgroupId1_
[14 Apr 2008 14:42:11] returning '429' as column: id1_
[14 Apr 2008 14:42:11]
/* load one-to-many domain.Subgroup.members */ select
members0_.subgroupId as subgroupId1_,
members0_.id as id1_,
members0_.id as id6_0_,
members0_.username as username6_0_,
members0_.admin as admin6_0_,
members0_.dateJoined as dateJoined6_0_,
members0_.addedBy as addedBy6_0_,
members0_.ccomment as ccomment6_0_,
members0_.subgroupId as subgroupId6_0_,
(SELECT
sg.name
FROM
lsf.subgroups sg
WHERE
sg.id = members0_.subgroupId) as formula13_0_
from
lsf.subgroup_members members0_
where
members0_.subgroupId=?
[14 Apr 2008 14:42:11] binding '287' to parameter: 1
[14 Apr 2008 14:42:11]
/* delete domain.Fraction */ delete
from
lsf.fractions
where
id=?
[14 Apr 2008 14:42:11] binding '429' to parameter: 1
[14 Apr 2008 14:42:11]
/* delete domain.Subgroup */ delete
from
lsf.subgroups
where
id=?
[14 Apr 2008 14:42:11] binding '287' to parameter: 1



cheers


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 9:29 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
This happens because of the cascade="all, delete-orphans". This means you are cascading all the operations (including delete) on the related tables. If you make cascade="none" this will not happen and delete query will be executed only for the subgroup table.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 14, 2008 9:39 am 
Newbie

Joined: Sun Feb 10, 2008 3:28 pm
Posts: 3
Sukirtha wrote:
This happens because of the cascade="all, delete-orphans". This means you are cascading all the operations (including delete) on the related tables. If you make cascade="none" this will not happen and delete query will be executed only for the subgroup table.



Well, that way I get an integrety constraint violated. Because there's a foreign key constraint between the Fraction table and Subgroup table as well as between Member table and Subgroup table (defined at DB level).

Basicaly I wan Hibernate to do the following:

get the ID of the Subgroup (parent)

then issue 2 delete statement like:

delete from fractions where subgroupId = parentId;
delete from members where subgroupId = parentId;

And NOT fetch the entire association with Fractions and Members.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 15, 2008 12:03 am 
Senior
Senior

Joined: Mon Feb 25, 2008 1:48 am
Posts: 191
Location: India
It doesn't happen that way. Hibernate always issue select queries before performing update or delete. However you can decide on when to issue the select queries by setting the lazy attribute to true or false.

_________________
Sukirtha


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2008 4:19 pm 
Newbie

Joined: Sun Sep 07, 2008 11:07 pm
Posts: 17
I am exactly have the same problem.
I am thinking Hibernate is smart enough only to execute the delete statements and not do the select.

Can anybody please let me know if I am missing any configuration


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