• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Hibernate performance

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys !

What is the best approach about hibernate fetch strategy, use LAZY or EAGER ? Is it better get all data in only once SQL (EAGER) or some SQL queries when you need the data (LAZY) ??

Regards,

Robert
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on the data and how you use it, there is no "one size fits all" approach to this.
 
Robert Siqueira
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ddo you have some source where I can study about this kind of performance hints ??

tks...
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Hibernate docs are a good place to start.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Sturrock wrote:The Hibernate docs are a good place to start.



Or if you understand French

http://arodrigues.developpez.com/tutoriels/java/performance/hibernate-performance-part1-strategies-chargement/
 
Sheriff
Posts: 3064
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The worst performance comes when you have LAZY on data that you will be using. So let's say your database has a USERS table and a related ADDRESS table. You retrieve a list of users, and you want to print out their addresses. Hibernate will do one query for the list of users and then one more query for the addresses for each of the users. That's a total of N+1 queries, where N is the number of users. Also know as the N+1 problem, this represents an epic failure in your mappings.

On the other hand, you could put EAGER on the addresses, but what if you rarely actually care about that data. The Hibernate query for users will include a join with the address table, which of course will be slower than a simple query of the users table. If you don't access the addresses, then you've wasted the effort to retrieve them.

Now, it is possible to write Hibernate (and JPA) queries to explicitly join in the addresses where you want them. That will override the LAZY load in the mappings. However, if you forget to do it, then you get the N+1 problem again.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic