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