2013-04-01 30 views
3

当我使用构造在HQL吹塑:HQL像 “选择新的构造”,在休眠casues mulitiple SQL执行

字符串JPQL =“选择顶部10新结果(A,B)来自A的,B b where a.id = b.id“ Query query = 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” 刚打电话使用方法 “查询查询= entityManager.createQuery(JPQL);”得到一些结果。

和休眠打印一些sql语句如下:

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=? 

但是,当“新目录”构造函数中使用的一些字段属性,如“新名单(a.name,b.score)”,它只能打印一个SQL。

+0

你的问题有点不清楚,发布两个查询与各自的结果。另外,'top'不是JPQL中的保留关键字,您不是创建本地查询,这是您的工作代码。 –

+0

感谢您的关注。我添加了一些细节,希望已经足够清晰。 – Taurus

回答

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的更多详细信息,我们可以在您的实体中指定关于此行为的准确解释以及如何更改此实例。