-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA select new using constructor results in n+1 SQL executed
PostPosted: Fri Jul 05, 2013 10:18 am 
Newbie

Joined: Fri Jul 05, 2013 9:55 am
Posts: 3
When using the following JPQL, I get n+1 queries executed:
Code:
List resultList = em.createQuery("select new com.example.dto.CountableEntryDto(p, count(dogs)) from Person p left join p.dogs dogs group by p.id").getResultList();

results in:

Code:
select person0_.id as col_0_0_, count(dogs1_.id) as col_1_0_ from Person person0_ left outer join Dog dogs1_ on person0_.id=dogs1_.owner_id group by person0_.id
select person0_.id as id32_0_, person0_.age as age32_0_, person0_.firstname as firstname32_0_, person0_.lastname as lastname32_0_ from Person person0_ where person0_.id=?
select person0_.id as id32_0_, person0_.age as age32_0_, person0_.firstname as firstname32_0_, person0_.lastname as lastname32_0_ from Person person0_ where person0_.id=?
select person0_.id as id32_0_, person0_.age as age32_0_, person0_.firstname as firstname32_0_, person0_.lastname as lastname32_0_ from Person person0_ where person0_.id=?

However this query results in only one query generated, which is what I want:

Code:
List resultList = em.createQuery("select p, count(dogs) from Person p left join p.dogs dogs group by p.id").getResultList();


results in
Code:
select person0_.id as col_0_0_, count(dogs1_.id) as col_1_0_, person0_.id as id10_, person0_.age as age10_, person0_.firstname as firstname10_, person0_.lastname as lastname10_ from Person person0_ left outer join Dog dogs1_ on person0_.id=dogs1_.owner_id group by person0_.id

I would like to be able to use "select new..." constructor in my query though, but why is the generated SQL so inefficient?

I'm using Hibernate 4.1.0.Final, Hibernate JPA2 1.0.1.Final and running against Postgres.


Top
 Profile  
 
 Post subject: Re: JPA select new using constructor results in n+1 SQL executed
PostPosted: Tue Jul 23, 2013 2:47 pm 
Newbie

Joined: Fri Jul 05, 2013 9:55 am
Posts: 3
Is no one else seeing this behavior? Can someone verify whether this happens?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.