Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate标准API等效于HQL select子句?_Java_Hibernate_Orm_Hql_Criteria - Fatal编程技术网

Java Hibernate标准API等效于HQL select子句?

Java Hibernate标准API等效于HQL select子句?,java,hibernate,orm,hql,criteria,Java,Hibernate,Orm,Hql,Criteria,我想要对两个持久类进行组合查询 在HQL中,这可以通过select子句实现 select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr 在上面的示例中,Family是一个conbined类,其construtor参数为DemesticCat HQL select子

我想要对两个持久类进行组合查询

在HQL中,这可以通过select子句实现

select new Family(mother, mate, offspr)
    from DomesticCat as mother
        join mother.mate as mate
        left join mother.kittens as offspr
在上面的示例中,Family是一个conbined类,其construtor参数为DemesticCat


HQL select子句的标准等价物是什么?

在标准API中,此功能由处理。文档有点混乱和过于复杂,但这正是您需要查看的内容。

为此,您必须使用。博客文章给出了以下示例(其中
StudentDTO
是一个非实体Bean):


斯卡夫曼:谢谢你的快速回答。我刚刚简要介绍了Projections部分,发现PropProjection.Property.forName(“name”)可以用来过滤列的子集作为结果,但是我找不到将两个持久性类映射到一个新类的投影,比如上面示例中的Family类?你能解释一下吗?非常感谢!那正是我要找的!似乎标准几乎和HQL一样强大。谢谢
List resultWithAliasedBean = s.createCriteria(Enrolment.class)
  .createAlias("student", "st").createAlias("course", "co")
  .setProjection( Projections.projectionList()
                   .add( Projections.property("st.name"), "studentName" )
                   .add( Projections.property("co.description"), "courseDescription" )
          )
          .setResultTransformer( Transformers.aliasToBean(StudentDTO.class) )
          .list();

 StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);