-->
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: Howto make Hibernate re-use already executed queries?
PostPosted: Mon Apr 26, 2010 6:18 pm 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Hi,

I've a quite complex piece of buissness-logic which executes quite a lot of database queries and because of that is painfully slow.
What I've done in some areas is to replace database-queries with HashMaps, fetching all data at the beginning into a HashMap,
and looking up the actually needed result by key. However thats a really bad thing for code-quality and maintainability :/

Is there some way to make hibernate re-use the results from an already executed query, if its obvious that it already has all the looked-up'ed items loaded before?

Something like:
Code:
   manager.createQuery("From Booking b WHERE b.nrOfTickets > 0 AND b.nrOfTickets < 100").getResultList();
   for(int i=1; i < 10; i++) {
      manager.createQuery("From Booking b WHERE b.nrOfTickets ="+i+"").getResultList(); //No need to ask the database
   }


Thank you in advance, Clemens


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Tue Apr 27, 2010 3:29 am 
Beginner
Beginner

Joined: Thu Feb 08, 2007 10:40 am
Posts: 46
You can use the second level cache and then make your query cacheable by calling query.setCacheable(true).


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Tue Apr 27, 2010 4:19 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Yes, but that only works if you execute the same query over and over, which is not the case.


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Tue Apr 27, 2010 5:47 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
You can use filters to narrow your resultset, but you are asking that Hibernate should interpreter your query and figure out if it should or should not execute, that's up to yourself.

Hibernate does make sure not to load the same entity 2 times using the session cache.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Tue Apr 27, 2010 10:31 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
Hi Dencel,

Do you know whats the "standard"-way of solving the too-many-queries-because-of-too-oop-heavy-code?
My code is OOP-heavy, e.g. calculating one item at a time - and because of the complexity of the buissness logic I don't dare to mess arround and destroy its structure.

Thanks, Clemens


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Tue Apr 27, 2010 6:20 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Yes, but that only works if you execute the same query over and over, which is not the case.

no, it's way more powerful. make sure you make the entities cacheable by PK, and collection relations cacheable, and queries too.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Howto make Hibernate re-use already executed queries?
PostPosted: Wed Apr 28, 2010 4:49 am 
Beginner
Beginner

Joined: Tue Jul 03, 2007 8:47 am
Posts: 46
I'll give it a try - Thanks!

- Clemens


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.