• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please help me avoid n+1 select hibernate

 
srini carry
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using hibernate for persistance. All my entities most od the associations are lazy and by default collections are lazy. I also have lot of criteria queries and because of lazy mapping I always face n+1 problem. Suppose I have item and bids the famous example by default bids are lazy , so in a criteria query If I select Items and I also needs bids for each item in this case I end up with n+1 select please tell me how can I avoid n+1 using criteria ?

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate is doing this behind the scenes for you, right? So, you're not doing multiple queries, but instead, somewhere in a place where you're not controlling program execution, these n+1 selects are happening, right?

Just confirm this for me so I have a clear picture of your problem.

-Cameron McKenzie
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know you need the associated objects why not eagerly fetch them?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic