简体   繁体   English

Hibernate Criteria API是否等同于HQL select子句?

[英]Hibernate Criteria API equivalent to HQL select clause?

I'd like to have a combined query for two persistent classes. 我想对两个持久性类进行合并查询。

In HQL this could be achieved by the select clause, 在HQL中,这可以通过select子句来实现,

select new Family(mother, mate, offspr)
    from DomesticCat as mother
        join mother.mate as mate
        left join mother.kittens as offspr

In the above example, Family is a conbined class with DemesticCat as its construtor params 在上面的示例中,Family是一个以DemesticCat作为其构造器参数的组合类

What is the Criteria equivalent of the HQL select clause ? HQL select子句的Criteria等效项是什么?

You'll have to use a ResultTransformer for this. 您必须为此使用ResultTransformer The Hibernate 3.2: Transformers for HQL and SQL blog post gives the following example (where StudentDTO is a non-entity Bean): Hibernate 3.2:HQL和SQL的变压器的博客文章给出了以下示例(其中StudentDTO是非实体Bean):

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);  

In the Criteria API, this functionality is handled by Projections . 在Criteria API中,此功能由Projections处理。 The documentation is a bit confusing and over-complicated, but that's what you need to look at. 该文档有点混乱和过于复杂,但这就是您需要查看的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM