当我在hql中使用Constructor作为打击:
<德尔> String jpql =“从A a,B b中选择前10个新结果(a,b),其中a.id = b.id” 查询查询= entityManager.createQuery(jpql); 德尔>控制台打印出20个sql语句。不像我预期的那样。但是当构造函数只包含表A和B的文件时,它只执行一个sql。我想知道为什么会这样。谢谢你的帮助!
很抱歉不清楚,这里有一些细节:
List list = baseDao.findListByQL("select new List(a,b) from A a,B b where a.id=b.id");
方法“findListByQL”刚刚调用了一个方法,使用“Query query = entityManager.createQuery(jpql);”得到一些结果。
并且hibernate打印一些sqls,如下所示:
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=?
但是当“new List”构造函数使用一些字段属性,如“new List(a.name,b.score)”时,它只打印一个sql。
答案 0 :(得分:0)
当您执行HQL时:
select new List(a,b) from A a,B b where a.id=b.i
您将返回整个实体A和B.即使没有看到这些实体的代码,也可能会映射一些 EAGER关系,例如@ManyToOne
或@OneToOne
。
在EAGER关系中,每当您点击具有此关系的实体时,它都会使实体关联。这可以解释这多个SQL。
但是,当您执行HQL时:
select new List(a.name,b.score) from A a,B b where a.id=b.i
您只返回一些字段。在这种情况下,EAGER关系被“忽略”,因为您没有使用实体类。
如果您提供有关实体类A和B的更多详细信息,我们可以在您的实体中指定有关此行为的确切说明以及如何更改此内容。