-->
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.  [ 8 posts ] 
Author Message
 Post subject: List Behavior
PostPosted: Tue Dec 12, 2006 12:12 pm 
Newbie

Joined: Tue Dec 12, 2006 12:02 pm
Posts: 5
I have tried the following with 3.1.3 and 3.2.1:

create table test_parent(
parent_key NUMERIC(3) not null,
description varchar(40),
hibernate_version numeric(17),
CONSTRAINT pk_test_parent PRIMARY KEY( parent_key ) );

create table test_child(
parent_key NUMERIC(3) not null,
child_key NUMERIC(3) not null,
description varchar(40),
hibernate_version numeric(17),
CONSTRAINT pk_test_child PRIMARY KEY( parent_key, child_key ) );

INSERT INTO test_parent
(
parent_key,
description,
hibernate_version
)
VALUES
(
1,
'Test Parent',
0
);

INSERT INTO test_child
(
parent_key,
child_key,
description,
hibernate_version
)
VALUES
(
1,
1,
'Test Child',
0
);

INSERT INTO test_child
(
parent_key,
child_key,
description,
hibernate_version
)
VALUES
(
1,
3,
'Test Child',
0
);

INSERT INTO test_child
(
parent_key,
child_key,
description,
hibernate_version
)
VALUES
(
1,
5,
'Test Child',
0
);

INSERT INTO test_child
(
parent_key,
child_key,
description,
hibernate_version
)
VALUES
(
1,
10,
'Test Child',
0
);

TestParent.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.TestParent" table="test_parent">
<id column="parent_key" name="parentKey" type="integer">
<generator class="assigned"/>
</id>

<version column="hibernate_version" name="hibernateVersion" type="long" unsaved-value="null"/>

<property column="description" length="40" name="description" type="string"/>

<list name="childList"
inverse="true"
lazy="true"
cascade="all-delete-orphan">

<key column="parent_key"/>

<list-index column="child_key"/>

<one-to-many class="hibernate.TestChild"/>
</list>
</class>
</hibernate-mapping>


TestChild.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.TestChild" table="test_child">

<composite-id>
<key-property column="parent_key" length="3" name="parentKey" type="integer"/>
<key-property column="child_key" length="3" name="childKey" type="integer"/>
</composite-id>

<version column="hibernate_version" name="hibernateVersion" type="long" unsaved-value="null"/>

<property column="description" length="40" name="description" type="string"/>
</class>
</hibernate-mapping>


Query and code:
Iterator iterator =
session.createQuery(
"SELECT parent FROM TestParent parent LEFT JOIN parent.childList" )
.list( ).iterator( );

while ( iterator.hasNext( ) )
{
TestParent testParent = (TestParent) iterator.next( );
System.err.println( testParent );
for ( TestChild testChild : testParent.getChildList( ) )
{
System.err.println( testChild );
}
}

Creates the following output:
hibernate.TestParent@fd66a5[parentKey=1]
null
hibernate.TestChild@1389b3f[parentKey=1,childKey=1]
null
hibernate.TestChild@1a0b53e[parentKey=1,childKey=3]
null
hibernate.TestChild@1dafb4e[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1a8d460[parentKey=1,childKey=10]
hibernate.TestParent@fd66a5[parentKey=1]
null
hibernate.TestChild@1389b3f[parentKey=1,childKey=1]
null
hibernate.TestChild@1a0b53e[parentKey=1,childKey=3]
null
hibernate.TestChild@1dafb4e[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1a8d460[parentKey=1,childKey=10]
hibernate.TestParent@fd66a5[parentKey=1]
null
hibernate.TestChild@1389b3f[parentKey=1,childKey=1]
null
hibernate.TestChild@1a0b53e[parentKey=1,childKey=3]
null
hibernate.TestChild@1dafb4e[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1a8d460[parentKey=1,childKey=10]
hibernate.TestParent@fd66a5[parentKey=1]
null
hibernate.TestChild@1389b3f[parentKey=1,childKey=1]
null
hibernate.TestChild@1a0b53e[parentKey=1,childKey=3]
null
hibernate.TestChild@1dafb4e[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1a8d460[parentKey=1,childKey=10]

My issues are:
1) I get the same results 4 times
2) The childList seems to fill in non existant values with null records

Could someone help me with this or is this expected behavior and if so could someone point me to a link so i can understand it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 12:16 pm 
Newbie

Joined: Tue Dec 12, 2006 12:02 pm
Posts: 5
here is the new query:
Iterator iterator =
session.createQuery(
"FROM TestParent parent LEFT JOIN FETCH parent.childList" )
.list( ).iterator( );

while ( iterator.hasNext( ) )
{
TestParent testParent = (TestParent) iterator.next( );
System.err.println( testParent );
for ( TestChild testChild : testParent.getChildList( ) )
{
System.err.println( testChild );
}
}

Here is the output:

hibernate.TestParent@14e4e31[parentKey=1]
null
hibernate.TestChild@3228a1[parentKey=1,childKey=1]
null
hibernate.TestChild@4d41e2[parentKey=1,childKey=3]
null
hibernate.TestChild@20807c[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1d38b87[parentKey=1,childKey=10]
hibernate.TestParent@14e4e31[parentKey=1]
null
hibernate.TestChild@3228a1[parentKey=1,childKey=1]
null
hibernate.TestChild@4d41e2[parentKey=1,childKey=3]
null
hibernate.TestChild@20807c[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1d38b87[parentKey=1,childKey=10]
hibernate.TestParent@14e4e31[parentKey=1]
null
hibernate.TestChild@3228a1[parentKey=1,childKey=1]
null
hibernate.TestChild@4d41e2[parentKey=1,childKey=3]
null
hibernate.TestChild@20807c[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1d38b87[parentKey=1,childKey=10]
hibernate.TestParent@14e4e31[parentKey=1]
null
hibernate.TestChild@3228a1[parentKey=1,childKey=1]
null
hibernate.TestChild@4d41e2[parentKey=1,childKey=3]
null
hibernate.TestChild@20807c[parentKey=1,childKey=5]
null
null
null
null
hibernate.TestChild@1d38b87[parentKey=1,childKey=10]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 1:41 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
if you use List collection, hibernate expects the index column to be contiguous. That is, it expects the index column to have 0, 1, 2, 3, 4, 5, ... However, if you have 0, 1, 3, 4, 7, 10 you will get back

object -0
object -1
null -2
null -3
object -4
null -5
null -6
object -8

etc

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 1:43 pm 
Newbie

Joined: Tue Dec 12, 2006 12:02 pm
Posts: 5
Is there a way not to get the null objects?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 1:53 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
yeah. in the parent setter for the collection, have it assign a contiguous list of index values.


Code:
setChildren(List list) {
int n = 0;
for (Iterator iter = list.iterator; iter.hasNext();) [
Object o = iter.next();
(MyObject o).setIndexValue(n);
n++;
}

//extend the parent class, then call it's method when you're done assisning stuff
super.setChildren(list);


_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 12, 2006 1:55 pm 
Newbie

Joined: Tue Dec 12, 2006 12:02 pm
Posts: 5
Thanks for your help chris


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:28 pm 
Newbie

Joined: Tue Sep 26, 2006 8:34 am
Posts: 16
Location: Paris, France
i finally solve my issue which look similar to your's

check http://forum.hibernate.org/viewtopic.php?t=968296


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 12:35 pm 
Newbie

Joined: Tue Dec 12, 2006 12:02 pm
Posts: 5
I finally went with just using a bag in the hbm.xml file and using a list<> in the POJO and it works great, but your solution seems that it would work also.
Hibernate definetly is flexible and robust


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