-->
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.  [ 14 posts ] 
Author Message
 Post subject: Object graph and dependants objects
PostPosted: Thu Jan 15, 2004 1:07 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi all,

Here is another new question about best practice of object mapping with Hibernate.

The question is: as it is possible to use the lazy loading pattern for associated collections, are there any similar solutions for many-to-one (or one-to-one) associated objects ?

Example:

My object model is described below:

(1) Class A n-->1 Class B

(2) Class B 1<-->1 Class C

(3) Class B 1<-->1 Class D

(4) Class B 1-->n Class E


My current mapping for (1) is a many-to-one association.

A big problem occurs when I want to load a list of objects of class A: hibenrate automatically loads every object B instance for each object A instance... and every object C instance for each object B instance etc...

This ends by very poor performance because some requests use more than 10 left outer joins :-(


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2004 1:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/Documentation/SomeExplanationsOnLazyLoading

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 4:19 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi emmanuel,

Thanks for your answer; I can see the constrained="false" attribute for the one-to-one element, but it doesn't seem existing for the many-to-one element...
Is there a similar solution for the many-to-one associated objects ?

Thanks a lot for your help.
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 4:21 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Just use lazy=true for your associations


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 4:31 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi,

The manual doesn't show any lazy attribute for the many-to-one element !

Or have I to unuse the many-to-one element ?????

From the documentation:

Code:
<many-to-one
        name="propertyName"                               
        column="column_name"                               
        class="ClassName"                                 
        cascade="all|none|save-update|delete"             
        outer-join="true|false|auto"                       
        update="true|false"                               
        insert="true|false"                               
        property-ref="propertyNameFromAssociatedClass"     
        access="field|property|ClassName"                 
/>


regards,
yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 4:35 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you want lazy loading on the one side, you will have to use a proxy for the class. This has nothing to do with inverse, which just controls saving/updating behaviour.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 6:00 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
I use Hibernate 2.0.3 and there is no lazy attribute for the class element.

What is the solution for this release ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 6:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
proxy="Classname"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 6:05 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Thanks. I don't find the documentation very useful on this point. My use case is not an inheritance model, so the Cat and DomesticCat example is not very useful to me.

So, when I set <class name="mypackage.myclass" proxy="mypackage.myclass" ...... />

Is lazy-initalization automatic ? Do I have to implement some new methods in mypackage.myclass to set the rules for the lazy-loading ?

Thanks for your answers

Regards,
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 6:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
yannraoul wrote:
Thanks. I don't find the documentation very useful on this point. My use case is not an inheritance model, so the Cat and DomesticCat example is not very useful to me.


Feel free to improve it.

yannraoul wrote:
So, when I set <class name="mypackage.myclass" proxy="mypackage.myclass" ...... />
Is lazy-initalization automatic ? Do I have to implement some new methods in mypackage.myclass to set the rules for the lazy-loading ?


Yes
No
See http://www.hibernate.org/hib_docs/reference/html/performance.html#performance-s4


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2004 5:54 pm 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
I have declared the class as the proxy class, but it doesn't seem to change anything to the way the associated objects are loaded.

Do I have to do anything ?
Has anybody has a simple example to illustrate the awaited behavior of the proxy feature ?

Thanks a lot !!!
Yann


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 17, 2004 9:17 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You proxied class B, let many-to-one to outer-join="auto", and when loading class A, B is actually loaded. Is that what you're saying ?
Hard to believe, there must be something else.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 6:10 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 11:36 am
Posts: 46
Location: Rennes, France
Hi,

What is the many-to-one association you are talking about ?

Here is my mapping file. When I load the an "Entreprise" object, Hibernate performs the request to get the "Employe" instances.

Ex:

Code:
Entreprise e = (Entreprise)session.load(Entreprise.class, new Long(1));


donne

Code:
Hibernate: select entrepri0_.entreprise_id as entreprise_id, entrepri0_.nom as nom from Entreprise entrepri0_ where entrepri0_.entreprise_id=?
Hibernate: select employe0_.emp_id as emp_id__, adresse1_.emp_id as emp_id0_, adresse1_.adresse as adresse0_, adresse1_.code_postal  as code_pos3_0_, adresse1_.ville as ville0_, employe0_.emp_id as emp_id1_, employe0_.nom as nom1_, employe0_.prenom as prenom1_, employe0_.boss_emp_id as boss_emp4_1_, employe0_.entreprise_id as entrepri5_1_ from employe employe0_, Adresse adresse1_ where employe0_.entreprise_id=? and employe0_.emp_id=adresse1_.emp_id(+)

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

   <class name="Periode" table="periode">
      <id name="id" column="periode_id" type="long">
         <generator class="sequence">
            <param name="sequence">periode_id_seq</param>
         </generator>
      </id>
      <property name="nom" column="periode" type="string"/>
   </class>


   <class name="Adresse" table="Adresse">
      <id name="id" column="emp_id" type="long">
         <generator class="native"/>
      </id>
      <property name="adresse" type="string"/>
      <property name="codePostal" column="code_postal   " type="string"/>
      <property name="ville" type="string"/>
   </class>
   
   <class name="Employe" table="employe" proxy="Employe">
      <id name="id" column="emp_id" type="long">
         <generator class="sequence">
            <param name="sequence">employe_id_seq</param>
         </generator>
      </id>
      <property name="nom" type="string"/>
      <property name="prenom" type="string"/>
      <one-to-one name="adresse" class="Adresse" />
      <many-to-one name="boss" class="Employe" column="boss_emp_id" />
      <set name="salaires" lazy="true" cascade="all">
         <key column="emp_id" />
         <one-to-many class="Salaire" />
      </set>
      <many-to-one name="entreprise" class="Entreprise" column="entreprise_id" />
   </class>
   
   <class name="Salaire" table="Salaire">
      <composite-id unsaved-value="any">
         <key-many-to-one name="employe" column="emp_id" class="Employe"/>
         <key-many-to-one name="periode" column="periode_id" class="Periode"/>
      </composite-id>
      <property name="salaire"  type="long"/>
   </class>
   <class name="Entreprise" table="Entreprise">
      <id name="id" column="entreprise_id" type="long">
         <generator class="sequence">
            <param name="sequence">entreprise_id_seq</param>
         </generator>
      </id>
      <property name="nom" type="string"/>
      <set name="employes" cascade="delete">
         <key column="entreprise_id" />
         <one-to-many class="Employe" />
      </set>
   </class>
   
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 7:20 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
entreprise does not have a many-to-one, you're not it the previous case.
Whant do you want to be loaded

_________________
Emmanuel


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