hql-like“;选择“新建构造函数”;在Hibernate中,执行多个sql

hql-like“;选择“新建构造函数”;在Hibernate中,执行多个sql,hibernate,hql,jpql,Hibernate,Hql,Jpql,当我将hql中的构造函数用作blow时: String jpql=“从a、b中选择前10个新结果(a、b),其中a.id=b.id” Query Query=entityManager.createQuery(jpql); 控制台打印出20条sql语句。不像我预期的那样一条都没有。但是当构造函数只包含表a和表B的文件时,它只执行一条sql。我想知道为什么会这样。谢谢你的帮助 抱歉,不清楚,以下是一些详细信息: List list = baseDao.findListByQL("select ne

当我将hql中的构造函数用作blow时:

String jpql=“从a、b中选择前10个新结果(a、b),其中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”刚刚调用了一个方法,使用“Query Query=entityManager.createQuery(jpql);”来获得一些结果

和hibernate打印一些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=?

但是,当“new List”构造函数使用一些字段属性(如“new List(a.name,b.score)”)时,它只打印一条sql。

当您执行HQL时,如:

select new List(a,b) from A a,B b where a.id=b.i
select new List(a.name,b.score) from A a,B b where a.id=b.i
您将返回整个实体A和B。即使看不到这些实体的代码,它们也可能映射了一些渴望的关系,如
@ManyToOne
@OneToOne

在渴望的关系中,每次您点击具有此关系的实体时,它都会带来关联的实体。这可以解释为什么会有多个SQL

但是,当您执行HQL时,如:

select new List(a,b) from A a,B b where a.id=b.i
select new List(a.name,b.score) from A a,B b where a.id=b.i
您只返回了一些字段。在本例中,由于您没有使用实体类,因此“忽略”了急切关系


如果您提供有关实体类别A和B的更多详细信息,我们可以指定您实体中这种行为的确切解释以及如何改变这种行为。

您的问题有点不清楚,请将这两个查询与各自的结果一起发布。另外,
top
在JPQL中不是保留关键字&您不是在创建本机查询,这是您的工作代码。谢谢您的关注。我添加了一些细节,希望足够清楚。