-->
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.  [ 6 posts ] 
Author Message
 Post subject: question on association graph
PostPosted: Fri Oct 15, 2004 11:34 am 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
I have a category. A category can have other children category, and a parent category.
This is the mapping file:

<?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" lazy="true" cascade="save-update" inverse="true">
<key column="category_id"/>
<one-to-many class="model.Category"/>
</set>

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

</hibernate-mapping>


Now in the source code i retireve a category with
Category cat = session.get(id);

then i close the session and i try to retry the parent, and the parent of the parent.
I expected to obtain an error, instead hibernate seems to have loaded the whole tree from the current category to the root (but not the children due to the lazy attribute setted to true in the children set).

I am a begginer so i would like to ask you if there is the possibility to lazy initialize also the many-to-one association.


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

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
all this is described in the reference guide

lazy attribute of class element + outer join of many to one....

read the reference guide please

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 1:34 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
I have read it, and I setted lazy=true and outer-join=false:


<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" lazy="true" cascade="save-update" inverse="true">
<key column="category_id"/>
<one-to-many class="model.Category"/>
</set>

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

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

And if I retrieve a categeroy that is a leaf of the tree hibernate perform N select, N = depth of the tree.
So this is not what i want.
Did i miss something?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:02 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
show document.hbm.xml

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:07 pm 
Regular
Regular

Joined: Tue Oct 12, 2004 9:02 am
Posts: 66
Location: Italy
<?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>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 15, 2004 2:13 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
change
<class name="model.Document" table="document">
to
<class lazy="true" name="model.Document" table="document">

change
<many-to-one name="parent" column="parent_id" class="model.Category" cascade="none" outer-join="false"/>
to
<many-to-one name="parent" column="parent_id" class="model.Category" cascade="none" outer-join="auto"/>

add a default constructor in Document

and next time read the reference guide carefully

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


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