-->
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: How hibernate load object from DB?
PostPosted: Fri Feb 20, 2004 10:35 am 
Newbie

Joined: Fri Feb 20, 2004 10:13 am
Posts: 5
Location: Italy
Hi,
I'm new using Hibernate 2.1.2, and I'm experiencing a performance problem.

I have two tables, Address and City, and they have a relationship "many-to-one" in the Address mapping file:

Code:
<many-to-one
        name="city"  class="com.test.hibernate.City"
        not-null="true">
        <column name="ID_CITY" />
</many-to-one>


When I retrieve the complete list of the Address object with the command:
Code:
hibSession.find("select add from Address add where add.id < 300 ")

Looking to the log i have seen that hibernate execute an initial query with the Select command on the table Address and then execute another query for each distinct City.

If I modify the query command in
Code:
hibSession.find("select add, add.city from Address add where add.id < 300 ")

it load the same object of type ind, but execute only a query with the correct join operator (inner or outer based on mapping).

Why this?

Is a behaviour by design or is a bug?

I would like that each object was loaded with a single SQL query... Can i control this with configuration parameter?


TIA,
Franco


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

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You have full control over how Hibernate loads objects. You can use lazy=true for your associations to only load the collection contents when accessing the collections.

You can also use "from Address add join fetch add.city" to load the associated citys by joins in the query.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 10:49 am 
Newbie

Joined: Fri Feb 20, 2004 10:13 am
Posts: 5
Location: Italy
My reference say that the tag "many-to-one" did not provide an attribute "lazy=true"!

I would like to access the Address object without listing in the query all other objects in the mapping file to workaround the problem.

Really I have experienced this problem with a chain more complex (six level of parent child).


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

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Take a look at http://www.hibernate.org/162.html, basically if you want lazy loading on the one-side, you have to use proxys. You can also use outer-join=true in the mapping to allways do outer-join fetching. Set hibernate.max_fetch_depth to a positive value if you do.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 10:58 am 
Regular
Regular

Joined: Tue Oct 14, 2003 11:11 pm
Posts: 62
Location: Brasil/Curitiba
fcaponi wrote:
My reference say that the tag "many-to-one" did not provide an attribute "lazy=true"!


you must define the associate class to use a proxy.

_________________
Ricardo Lecheta


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 20, 2004 11:19 am 
Newbie

Joined: Fri Feb 20, 2004 10:13 am
Posts: 5
Location: Italy
Thanks to all!!!

With this parameter i have obtained that hibernate don't load any referenced table until i need them... but can i have problems if i set the proxy attribute on each mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 9:23 am 
Newbie

Joined: Fri Feb 20, 2004 10:13 am
Posts: 5
Location: Italy
I'm experiencing problems with proxies because when i access list from my views, session is already closed.

Reading chapter 3.5.2. Outer Join Fetching i have seen that what i need can be configured with:
Code:
<property name="hibernate.use_outer_join">true</property>
<property name="hibernate.max_fetch_depth">3</property>

and on the many-to-one relationship the parameter outer-join has been forced to "true" (also if the auto value can be sufficient) but it don't work.

From log i have seen that outer join whas used on other command after first... It's correct or is a bug?

Any Ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 24, 2004 4:57 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you reformulate please.

using many-to-one auto with a proxied one side, won't load it, as per the doc.

_________________
Emmanuel


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.