0

Let's say I have an Entity A that has subentities B & C in one to many, and B and C in turn have a number of subentities and so on. I load A, and tell it to prefetch all subentities, but that takes too long. The crossproduct gets too large. It's not that bad if i load A->B->B's hierarchy in a HQL query, nor if I load A->C->C's hierarchy in a query.

So I would like to load A and it's hierarchy, but in multiple steps rather than one HQL. The objective is to load them all into one bean representing the header entity A. Can Hibernate do that? If so, how would I get that done? Currently I use the Query objects and am not into the criteria language, so via HQL is preferred but i'll do it via criteria if I have to.

Anyone have ideas?

Entropy
  • 1,219
  • 6
  • 21
  • 45
  • Is doing lazy loading an option? –  Jan 28 '20 at 22:05
  • https://stackoverflow.com/questions/97197/what-is-the-n1-selects-problem-in-orm-object-relational-mapping – Elliott Frisch Jan 28 '20 at 22:57
  • @jhell - Not really. We limit the transaction scope to the service call. I could iterate through the getB() type methods to provoke loads before leaving the service, but then I'd be in N+1 hell. My current problem is that the cartesian product of ONE query is too heavy, but N+1 wouldn't be any better most likely. So I am looking for a BALANCE. Each half of my object graph isn't that bad, it's together that they become painful. – Entropy Jan 29 '20 at 13:09
  • @ElliottFrisch - That link seems to be about N+1. I'm well aware of that performance trap, which is why my project tends to prefer letting hibernate query everything we'll need at once. The cartesian product usually is pretty manageable. But we have a situation where it isn't. I need a solution between these extremes. If I could just run half the tables in one query, then half in the next, but present the code with one loaded object it would be ideal. Hence the question. – Entropy Jan 29 '20 at 13:13

0 Answers0