-->
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.  [ 7 posts ] 
Author Message
 Post subject: retrieving an object with associations
PostPosted: Fri Oct 15, 2004 10:12 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
I don't understand why this code retrieve only one document from the category:

public void execute() {
Transaction tx = null;
Category category = null;

try {
int categoryID = 3;

Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();

category = (Category) session.get(Category.class,
new Long(categoryID));
Hibernate.initialize(category.getDocuments());

tx.commit();
} catch (HibernateException he) {
try {
tx.rollback();
} catch (HibernateException he2) {
he2.printStackTrace();
}

he.printStackTrace();
} finally {
try {
HibernateUtil.closeSession();
} catch (HibernateException he) {
he.printStackTrace();
}
}

System.out.println(category.getName());

Iterator iterator = category.getDocuments().iterator();

while (iterator.hasNext()) {
Document doc = (Document) iterator.next();
System.out.println(doc);
}
}


result is:

the name of category
the id of a document

Instead in my document table there are 3 document with category_id=3.

Here is the mapping for category:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="model.Category" table="category">
<id name="id" column="category_id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="description" type="string"/>

<set name="children">
<key column="category_id"/>
<one-to-many class="model.Category"/>
</set>

<many-to-one name="parent" column="parent_id" class="model.Category" cascade="none"/>

<set name="documents" inverse="true" cascade="save-update" lazy="true">
<key column="document_id"/>
<one-to-many class="model.Document"/>
</set>
</class>
</hibernate-mapping>


The mapping for Document instead is:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="model.Document" table="document">
<id name="id" column="document_id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string"/>
<property name="description" type="string"/>
<property name="text" type="text"/>
<many-to-one name="category"
class="model.Category"
column="category_id"
not-null="true"/>
</class>
</hibernate-mapping>


So the problem is that session.get retrieve only one document, instead it should retrieve 3 documents. Why?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 10:38 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
what about the generated sql? is it right?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 10:41 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
i post it...but i don't understand it...it is not much readable! (is it normal?)


[main] Hibernate: select category0_.category_id as category1_1_, category0_.name as name1_,
category0_.description as descript3_1_, category0_.parent_id as parent_id1_, category1_.category_
id as category1_0_, category1_.name as name0_, category1_.description as descript3_0_, category1_
.parent_id as parent_id0_ from category category0_ left outer join category category1_ on categor
y0_.parent_id=category1_.category_id where category0_.category_id=?
[main] Hibernate: select children0_.category_id as category1___, children0_.category_id as c
ategory1_1_, children0_.name as name1_, children0_.description as descript3_1_, children0_.parent
_id as parent_id1_, category1_.category_id as category1_0_, category1_.name as name0_, category1_
.description as descript3_0_, category1_.parent_id as parent_id0_ from category children0_ left o
uter join category category1_ on children0_.parent_id=category1_.category_id where children0_.cat
egory_id=?
[main] Hibernate: select documents0_.document_id as document1___, documents0_.document_id as
document1_2_, documents0_.name as name2_, documents0_.description as descript3_2_, documents0_.t
ext as text2_, documents0_.category_id as category5_2_, category1_.category_id as category1_0_, c
ategory1_.name as name0_, category1_.description as descript3_0_, category1_.parent_id as parent_
id0_, category2_.category_id as category1_1_, category2_.name as name1_, category2_.description a
s descript3_1_, category2_.parent_id as parent_id1_ from document documents0_ left outer join cat
egory category1_ on documents0_.category_id=category1_.category_id left outer join category categ
ory2_ on category1_.parent_id=category2_.category_id where documents0_.document_id=?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 10:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
copy paste the second query into db client and check number of rows returned

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 10:57 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
only one row returned!
So the sql is wrong...but i don't know why...and i am frustrated because this should be a very common and simple operation!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:07 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
<key column="document_id"/>

are you sure of this?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 11:18 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
Ehehe, sorry!
I have corrected it now and everything is ok!
Thanks!


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