This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes self referencing relationship - many selects from hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "self referencing relationship - many selects from hibernate" Watch "self referencing relationship - many selects from hibernate" New topic
Author

self referencing relationship - many selects from hibernate

Billy Vandory
Ranch Hand

Joined: Mar 23, 2010
Posts: 57
I'm using JPA with hibernate as the provider. I've implemented an entity with a self referencing relationship in order to be
able to do nested categories. When I load the root node, hibernate generates one select for each entity in the tree in order
to load the children. Im beginning to think, by the very nature of this type of tree, that this is unavoidable. can someone confirm?

Here's the mapping:



and here's the query. I just query the root node. Maybe this part is wrong



Here's the data:



and here's what hibernate does.



Too many selects (1 per entity to load all the children)

So, am I doing something wrong?
Shannon McGee
Greenhorn

Joined: Oct 01, 2009
Posts: 18
Hi Billy,

Since they are all seperate entities, I dont think there is a way around that. However, hibernate should help reduce the impact of all the calls w/ the shared cache and connection pooling.

-Shannon
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: self referencing relationship - many selects from hibernate
 
It's not a secret anymore!