Accessing child entities after QueryDSL join still causes lazy loading

2,937 views
Skip to first unread message

pete

unread,
Jun 25, 2012, 10:07:07 AM6/25/12
to quer...@googlegroups.com
Hej,

I'm wondering: Does QueryDSL put the entities I query for into the hibernate context/cache so that they can be accessed from there? I'm thinking, if I run a join for an entity, explicitly collecting all child relations, I should be able to save on the lazy loadings. I just can't figure out how..

Example: A has Bs and B has Cs. Now if I build my query to fetchAll() for a certain A and also join it with Bs and Cs:
List<A> as = query.from(QA.a).leftJoin(QA.a.b,QB.b).leftJoin(QB.b.c,QC.c).where(a.id.eq("1)).fetchAll();

 I will get a nice SQL statement that says

select... from A inner join B ... inner join C ... where ...

So the result should be sufficient for hibernate to initialize

as.get(0).getBs()
as.get(0).getBs().get(0).getCs()

but it won't do it, instead it will run querries to

select B where B.a = A.id...


Is it possible at all to reduce these lazy loadings by initializing an entity with all children with one join or am I hoping for too much?


Also: If I load all the data with a join, will hibernate have them in its cache and will it access that cache for those lazy loadings or will it poll the database? Do I get a say in that or is it completely out of my hands?


Thanks @ all!

Timo Westkämper

unread,
Jun 26, 2012, 1:14:46 AM6/26/12
to Querydsl on behalf of pete
Hi.

On Mon, Jun 25, 2012 at 5:07 PM, Querydsl on behalf of pete <quer...@googlegroups.com> wrote:
Hej,

I'm wondering: Does QueryDSL put the entities I query for into the hibernate context/cache so that they can be accessed from there? I'm thinking, if I run a join for an entity, explicitly collecting all child relations, I should be able to save on the lazy loadings. I just can't figure out how..

Querydsl JPA is just a thin wrapper on top of the JPA API, it doesn't have an own cache.
 

Example: A has Bs and B has Cs. Now if I build my query to fetchAll() for a certain A and also join it with Bs and Cs:
List<A> as = query.from(QA.a).leftJoin(QA.a.b,QB.b).leftJoin(QB.b.c,QC.c).where(a.id.eq("1)).fetchAll();

 I will get a nice SQL statement that says

select... from A inner join B ... inner join C ... where ...

So the result should be sufficient for hibernate to initialize

as.get(0).getBs()
as.get(0).getBs().get(0).getCs()

but it won't do it, instead it will run querries to

select B where B.a = A.id...


Is it possible at all to reduce these lazy loadings by initializing an entity with all children with one join or am I hoping for too much?


fetchAll() should do the job, the additional joins are not necessary. But I believe you need to have annotated accessors to make it work
 


Also: If I load all the data with a join, will hibernate have them in its cache and will it access that cache for those lazy loadings or will it poll the database? Do I get a say in that or is it completely out of my hands?

Just declaring joins without fetch directives will probably not affect the lazy loading.
 


Thanks @ all!



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com



Reply all
Reply to author
Forward
0 new messages