3
votes

When I'm using Constructor in hql as blow:

String jpql = "select top 10 new Result(a,b) from A a,B b where a.id=b.id" Query query = entityManager.createQuery(jpql);

The console print out 20 sql statements.not a single one as I expected.But when the constructor only contains Table A and B's fileds,it only execute one sql. I wonder why is that happened.Thank's for your help!

Sorry for not to be clear,here are some details:

List list = baseDao.findListByQL("select new List(a,b) from A a,B b where a.id=b.id");

the method " findListByQL" just called a method using "Query query = entityManager.createQuery(jpql);" to get some result.

and hibernate print some sqls as follows:

Hibernate: select a0_.id as col_0_0_, b1_.id as col_1_0_ from dbo.A a0_, dbo.B b1_ where a0_.id=b1_.id
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?
Hibernate: select a0_.id as id103_0_, a0_.name as name103_0_ from dbo.A a0_ where a0_.id=?
Hibernate: select b0_.id as id106_0_, b0_.score as score106_0_ from dbo.B b0_ where b0_.id=?

But when the "new List" constructor use some field attributes like "new List(a.name,b.score)" , it only print one sql.

1
Your question is bit unclear, post both the queries with respective results. Also, top isn't reserved keyword in JPQL & you aren't creating native query, is this your working code. - Nayan Wadekar
Thanks for your attention. I added some details and hope that's enough clear. - Taurus

1 Answers

0
votes

When you do a HQL like:

select new List(a,b) from A a,B b where a.id=b.i

You are returning the entire entity A and B. Even without see the code of these entities, probably they have some EAGER relationship mapped, like @ManyToOne or @OneToOne.

In an EAGER relationship, every time you hit a entity with this relationship, it will bring the entity associated. This can explain this multiple SQLs.

But, when you do a HQL like:

select new List(a.name,b.score) from A a,B b where a.id=b.i

You are returning only some fields. In this case, the EAGER relationship are "ignored", because you are not working with the entity class.

If you provide more details about the entity class A and B, we can appoint the exactly explanation about this behaviour in your entities and how change this.