简体   繁体   中英

Hibernate: Split up queries

(JPA/Hibernate question) I have a query to one table, let's call it "Release", from which I would like to retrieve ONE row, to be mapped to an entity. This entity then has five one-to-many associations, which will all be used in the near future, so I would like to eager fetch them. Now, each of these one-to-many associations has an amount of rows anywhere between 3 and 300. Altogether, the query would actually load some one million rows, if actually executed.

So, a little preview onto the generalized HQL query (some comments in brackets):

select rv, al, ... (blabla) from 
from Release rv 
left outer join fetch App app (on releaseId)
left outer join fetch Appconfig ac (on appId)
left outer join fetch Appbind ab (on appId)
left outer join fetch Releaseconfig rc (on releaseId)
left outer join fetch Somestuff st (on releaseId)
left outer join fetch Points p (on releaseId)
left outer join fetch Pointconfig pc (on pointId)
left outer join fetch Pointstuff ps (on pointId)
where rv.releaseId = :id

Now, say, App has 100 lines, and Appconfig and Appbind each 20 for each appId; Releasconfig about 15, Somestuff about 20, Points another 130, Pointconfig about 30 for each Point and Pointstuff about 50 for each Point. That will result in a MASSIVE query!

Is there a way for me to split up the queries and let Hibernate map them? Say, I ask for the App, Appconfig and Appbind first, then the Releaseconfig by itself and Somestuff by itself, and then Points, Pointconfig and Poinstuff altogether...

Thanks, Chris

You could to this by loading the required data upfront using a query as you have shown eg App, Appconfig and Appbind. All of the other collections which you haven't forced Hibernate to load will be represented by proxies.

Then when you need the next collection you could use Hibernate.initialize() passing in the proxy to force Hibernate to load it. This will require a separate query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM