简体   繁体   English

休眠条件内联接以选择

[英]Hibernate criteria Inner join to select

I have to translate the next expression into hibernate criteria api: 我必须将下一个表达式转换为休眠条件api:

select * from games gs
INNER JOIN (select gg.ID, gg.CODE, max(gg.DATE) max_date from games gg 
where gg.player_id = 1 group by gg.ID, gg.CODE) ss
 on gs.CODE = ss.CODE
 and gs.ID = ss.ID
 and gs.DATE = ss.max_date
 and gs.player_id = 1

How can I do it? 我该怎么做? I'm able to create inner and outer criteria separetely, but have no idea how to join them: 我能够分别创建内部和外部条件,但不知道如何将它们加入:

    DetachedCriteria innerCriteria = DetachedCriteria.forClass(Games.class, "gg");
    innerCriteria.add(Restrictions.eq("playerId", 1));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.groupProperty("id"));
    projectionList.add(Projections.groupProperty("code"));
    projectionList.add(Projections.max("date"));
    innerCriteria.setProjection(projectionList);

... ...

    Criteria criteria = getSession().createCriteria(Games.class, "gs");
    criteria.add(Restrictions.eq("playerId", 1));
    criteria.setProjection(Projections.id());
    criteria.list();

Hibernate does not support subqueries in the from clause. Hibernate在from子句中不支持子查询。 From the Hibernate 4.3 documentation : Hibernate 4.3文档中

Note that HQL subqueries can occur only in the select or where clauses. 请注意,HQL子查询只能在select或where子句中发生。

There is a pending feature request for this. 有一个待处理的功能请求

You'll have to rewrite the query to avoid that, or use native queries. 您必须重写查询以避免这种情况,或者使用本机查询。

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

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