-->
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: How to improve the performance?
PostPosted: Wed May 07, 2008 8:41 am 
Newbie

Joined: Fri Nov 02, 2007 9:27 am
Posts: 5
Hi,

I need of a suggestion to improve the performance of my code.

For example: I have two entities, User and Address. Each User has a Address attribute, as LAZY association. This model is ok, because only sometimes I need to read the Address from a User.

But in a screen of my system I need to simulate a EAGER. I used the Hibernate.initialize() and everything works fine (Below is my sample working code).

Code:
 
...
String str_query = "select u from User u"; 
Query query = session.createQuery(str_query); 
List<User> list = query.list(); 
 
for(User user: list){   
          Hibernate.initialize(user.getAddress()); 

 
return list; 
...


My doubt is: How to improve this code? Because, if I have 1000 Users and 1000 Addresses, the Hibernate will do 1001 SQLs (1 sql to list all users, and 1000 sqls to retrieve the addresses).

There are a way to tell to hibernate to do a single SQL to retrieve all required addresses?

Please, help me!

Thanks a lot!

Claudiney


Top
 Profile  
 
 Post subject: Re: How to improve the performance?
PostPosted: Wed May 07, 2008 9:22 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
It should work this way:

Code:
 
...
String str_query = "from User u join fetch u.Address"; 
Query query = session.createQuery(str_query); 
List<User> list = query.list(); 
 
return list; 
...


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 9:46 am 
Beginner
Beginner

Joined: Fri May 18, 2007 10:28 am
Posts: 48
Location: Madison, WI
If you have a lot of records, along with above suggestion u can set up a Hibernate Fetching strategy and get records as batches. As well Hibernate has good caching support.

http://www.devx.com/dbzone/Article/29685 is a decent one that can give some idea how to use secondary cache.

_________________
Please rate if it helped


Top
 Profile  
 
 Post subject: Re: How to improve the performance?
PostPosted: Wed May 07, 2008 11:07 am 
Newbie

Joined: Fri Nov 02, 2007 9:27 am
Posts: 5
typhos wrote:
It should work this way:

Code:
 
...
String str_query = "from User u join fetch u.Address"; 
Query query = session.createQuery(str_query); 
List<User> list = query.list(); 
 
return list; 
...


This way realy works fine, but now I have other problem:

Since that each Address has a State attribute defined as EAGER. The hibernate generates SQLs to retrieve each state.

is It possible increase more?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 2:17 am 
Regular
Regular

Joined: Thu Mar 06, 2008 5:06 am
Posts: 68
I think, you can fetch it the same way:

Code:
String str_query = "from User u join fetch u.Address a join fetch a.State";
Query query = session.createQuery(str_query);
List<User> list = query.list();

return list;


Try it out...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 08, 2008 2:36 am 
Regular
Regular

Joined: Wed Apr 25, 2007 11:44 pm
Posts: 59
typhos wrote:
I think, you can fetch it the same way:

Code:
String str_query = "from User u join fetch u.Address a join fetch a.State";
Query query = session.createQuery(str_query);
List<User> list = query.list();

return list;


Try it out...


yes you are right this is doing the same thing in one


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.