Hi all.
I've been looking for this but... so I take my chances and post.
MySQL 4.0.17, Spring framework RC 1.1, Hibernate 2.1.6, JUnit 3.8.1, ANT 1.6
AvikUser object holding a (composite) List of UserFunctionNode s referencing Function object, i.e basic relational object with a few extra fields.
Problem: JUnit testing a Spring-managed DAO results in a very unexpected, and extremely unwanted Hibernate-generated delete clause.
Here's the stack trace:
Code:
[junit] ======================testGetFunction======================
[junit] Hibernate: select avikuser0_.id as id0_, avikuser0_.fname as fname0_, avikuser0_.lname as lname0_, avikuser0
_.descr as descr0_, avikuser0_.email as email0_, avikuser0_.uname as uname0_, avikuser0_.pword as pword0_, avikuser0_.us
errole as userrole0_, avikuser0_.active as active0_, avikuser0_.listable as listable0_ from MASTERUSER avikuser0_ where
avikuser0_.id=?
[junit] Hibernate: select functions0_.user_fk as user_fk__, functions0_.func_fk as func_fk__, functions0_.fromdate a
s fromdate__, functions0_.todate as todate__, functions0_.iscurrent as iscurrent__, function1_.id as id0_, function1_.fu
ncdesc as funcdesc0_, function1_.active as active0_ from user2function functions0_ left outer join function function1_ o
n functions0_.func_fk=function1_.id where functions0_.user_fk=?
[junit] Hibernate: select delegated0_.user_fk as user_fk__, delegated0_.event_fk as event_fk__, delegated0_.delegate
_fk as delegate3___, delegated0_.validuntil as validuntil__, delegated0_.deleglevel as deleglevel__, event1_.id as id0_,
event1_.eventtype as eventtype0_, event1_.eventstatus as eventsta3_0_, event1_.created as created0_, event1_.changed as
changed0_, event1_.eventvisible as eventvis6_0_, event1_.diarynb as diarynb0_, event1_.evdate as evdate0_, event1_.evti
me as evtime0_, event1_.evdept as evdept0_, event1_.evloc as evloc0_, event1_.evdet as evdet0_, event1_.discdate as disc
date0_, event1_.disctime as disctime0_, event1_.discdept as discdept0_, event1_.discloc as discloc0_, event1_.discdet as
discdet0_, event1_.repdept as repdept0_, event1_.reppos as reppos0_, event1_.repname as repname0_, event1_.repdate as r
epdate0_, event1_.owner as owner0_, event1_.patname as patname0_, event1_.patnr as patnr0_, event1_.patinfo as patinfo0_
, event1_.patcomment as patcomment0_, event1_.bossentry as bossentry0_, event1_.bossread as bossread0_, event1_.reviewed
as reviewed0_, event1_.closed as closed0_, event1_.bossdate as bossdate0_, event1_.ev_descr as ev_descr0_, event1_.ev_w
hy as ev_why0_, event1_.ev_conseq as ev_conseq0_, event1_.ev_prevent as ev_prevent0_, event1_.ev_aux as ev_aux0_, depart
ment2_.id as id1_, department2_.itemcode as itemcode1_, department2_.itemorder as itemorder1_, department2_.itemval as i
temval1_, department2_.active as active1_, location3_.id as id2_, location3_.itemcode as itemcode2_, location3_.itemorde
r as itemorder2_, location3_.itemval as itemval2_, location3_.active as active2_, department4_.id as id3_, department4_.
itemcode as itemcode3_, department4_.itemorder as itemorder3_, department4_.itemval as itemval3_, department4_.active as
active3_, location5_.id as id4_, location5_.itemcode as itemcode4_, location5_.itemorder as itemorder4_, location5_.ite
mval as itemval4_, location5_.active as active4_, department6_.id as id5_, department6_.itemcode as itemcode5_, departme
nt6_.itemorder as itemorder5_, department6_.itemval as itemval5_, department6_.active as active5_, position7_.id as id6_
, position7_.itemcode as itemcode6_, position7_.itemorder as itemorder6_, position7_.itemval as itemval6_, position7_.ac
tive as active6_, function8_.id as id7_, function8_.funcdesc as funcdesc7_, function8_.active as active7_, function9_.id
as id8_, function9_.funcdesc as funcdesc8_, function9_.active as active8_ from delegation delegated0_ left outer join e
vent event1_ on delegated0_.event_fk=event1_.id left outer join Department department2_ on event1_.evdept=department2_.i
d left outer join Location location3_ on event1_.evloc=location3_.id left outer join Department department4_ on event1_.
discdept=department4_.id left outer join Location location5_ on event1_.discloc=location5_.id left outer join Department
department6_ on event1_.repdept=department6_.id left outer join Position position7_ on event1_.reppos=position7_.id lef
t outer join function function8_ on event1_.owner=function8_.id left outer join function function9_ on delegated0_.deleg
ate_fk=function9_.id where delegated0_.user_fk=?
[junit] Hibernate: select function2_.id as id, function2_.funcdesc as funcdesc, function2_.active as active, functio
ns1_.user_fk as user_fk__, functions1_.func_fk as func_fk__, functions1_.fromdate as fromdate__, functions1_.todate as t
odate__, functions1_.iscurrent as iscurrent__ from MASTERUSER avikuser0_ left outer join user2function functions1_ on av
ikuser0_.id=functions1_.user_fk, function function2_ where functions1_.func_fk=function2_.id and ((functions1_.iscurrent
=1 )and(avikuser0_.id=? ))
[junit] Hibernate: select delegated0_.user_fk as user_fk__, delegated0_.event_fk as event_fk__, delegated0_.delegate
_fk as delegate3___, delegated0_.validuntil as validuntil__, delegated0_.deleglevel as deleglevel__, event1_.id as id0_,
event1_.eventtype as eventtype0_, event1_.eventstatus as eventsta3_0_, event1_.created as created0_, event1_.changed as
changed0_, event1_.eventvisible as eventvis6_0_, event1_.diarynb as diarynb0_, event1_.evdate as evdate0_, event1_.evti
me as evtime0_, event1_.evdept as evdept0_, event1_.evloc as evloc0_, event1_.evdet as evdet0_, event1_.discdate as disc
date0_, event1_.disctime as disctime0_, event1_.discdept as discdept0_, event1_.discloc as discloc0_, event1_.discdet as
discdet0_, event1_.repdept as repdept0_, event1_.reppos as reppos0_, event1_.repname as repname0_, event1_.repdate as r
epdate0_, event1_.owner as owner0_, event1_.patname as patname0_, event1_.patnr as patnr0_, event1_.patinfo as patinfo0_
, event1_.patcomment as patcomment0_, event1_.bossentry as bossentry0_, event1_.bossread as bossread0_, event1_.reviewed
as reviewed0_, event1_.closed as closed0_, event1_.bossdate as bossdate0_, event1_.ev_descr as ev_descr0_, event1_.ev_w
hy as ev_why0_, event1_.ev_conseq as ev_conseq0_, event1_.ev_prevent as ev_prevent0_, event1_.ev_aux as ev_aux0_, depart
ment2_.id as id1_, department2_.itemcode as itemcode1_, department2_.itemorder as itemorder1_, department2_.itemval as i
temval1_, department2_.active as active1_, location3_.id as id2_, location3_.itemcode as itemcode2_, location3_.itemorde
r as itemorder2_, location3_.itemval as itemval2_, location3_.active as active2_, department4_.id as id3_, department4_.
itemcode as itemcode3_, department4_.itemorder as itemorder3_, department4_.itemval as itemval3_, department4_.active as
active3_, location5_.id as id4_, location5_.itemcode as itemcode4_, location5_.itemorder as itemorder4_, location5_.ite
mval as itemval4_, location5_.active as active4_, department6_.id as id5_, department6_.itemcode as itemcode5_, departme
nt6_.itemorder as itemorder5_, department6_.itemval as itemval5_, department6_.active as active5_, position7_.id as id6_
, position7_.itemcode as itemcode6_, position7_.itemorder as itemorder6_, position7_.itemval as itemval6_, position7_.ac
tive as active6_, function8_.id as id7_, function8_.funcdesc as funcdesc7_, function8_.active as active7_, function9_.id
as id8_, function9_.funcdesc as funcdesc8_, function9_.active as active8_ from delegation delegated0_ left outer join e
vent event1_ on delegated0_.event_fk=event1_.id left outer join Department department2_ on event1_.evdept=department2_.i
d left outer join Location location3_ on event1_.evloc=location3_.id left outer join Department department4_ on event1_.
discdept=department4_.id left outer join Location location5_ on event1_.discloc=location5_.id left outer join Department
department6_ on event1_.repdept=department6_.id left outer join Position position7_ on event1_.reppos=position7_.id lef
t outer join function function8_ on event1_.owner=function8_.id left outer join function function9_ on delegated0_.deleg
ate_fk=function9_.id where delegated0_.user_fk=?
[junit] [color=red]Hibernate: delete from user2function where user_fk=?[/color]
[junit] ======================testGetFunction======================
Mapping (part of) for AvikUser:
Code:
<hibernate-mapping>
<class
name="ks.rah.avik2.domain.AvikUser"
table="MASTERUSER"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>
<property
name="fname"
type="java.lang.String"
update="true"
insert="true"
column="fname"
/>
etc
etc
etc
<bag
name="functions"
table="user2function"
lazy="false"
inverse="false"
cascade="save-update"
>
<key
column="user_fk"
/>
<composite-element
class="ks.rah.avik2.domain.support.UserFunctionNode"
>
<many-to-one
name="function"
class="ks.rah.avik2.domain.Function"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="func_fk"
/>
<property
name="from"
type="date"
update="true"
insert="true"
column="fromdate"
/>
<property
name="until"
type="date"
update="true"
insert="true"
column="todate"
/>
<property
name="current"
type="java.lang.Boolean"
update="true"
insert="true"
column="iscurrent"
/>
</composite-element>
</bag>
</class>
Mapping for Function:
Code:
<hibernate-mapping>
<class
name="ks.rah.avik2.domain.Function"
table="function"
dynamic-update="false"
dynamic-insert="false"
>
<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
</generator>
</id>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="funcdesc"
/>
<property
name="active"
type="boolean"
update="true"
insert="true"
column="active"
/>
<bag
name="delegated"
table="delegation"
lazy="false"
inverse="false"
cascade="save-update"
>
<key
column="user_fk"
/>
<composite-element
class="ks.rah.avik2.domain.Delegation"
>
<many-to-one
name="event"
class="ks.rah.avik2.domain.Event"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="event_fk"
/>
<many-to-one
name="delegate"
class="ks.rah.avik2.domain.Function"
cascade="none"
outer-join="auto"
update="true"
insert="true"
column="delegate_fk"
/>
</composite-element>
</bag>
</class>
</hibernate-mapping>
Calling JUnit testcase method:
Code:
public void testGetFunction() throws HibernateException
{
System.out.println("======================testGetFunction======================");
user = logic.loadUser(42);
this.assertNotNull("user is NULL", user);
Function f = logic.getCurrentFunction(user);
this.assertNotNull("f is NULL", f);
this.assertEquals(f.getDescription(), "Test");
System.out.println("======================testGetFunction======================");
}
The call to
Code:
user = logic.loadUser(42);
does not generate any delete clauses. I tested it separately.
Invoked method in DAO:
Code:
public Function getCurrentFunction(final AvikUser user) throws DataAccessException{
List out = getHibernateTemplate().find(
"select n.function from AvikUser u left join fetch " +
"u.functions n where n.current = 1 and u.id = ?",
new Object[] {user.getId()},
new Type[] {Hibernate.LONG});
if( out == null ) return null;
else return (Function) out.get(0);
}
How is it possible that this generates a delete clause!?
Anyone see what I'm doing wrong here or missing?
Thanks!
/C