Hibernate标准内连接选择

时间:2016-02-09 12:35:42

标签: java hibernate criteria-api

我必须将下一个表达式翻译成hibernate criteria 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

我该怎么办?我能够单独创建内部和外部标准,但不知道如何加入它们:

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

1 个答案:

答案 0 :(得分:0)

Hibernate不支持from子句中的子查询。来自Hibernate 4.3 documentation

  

请注意,HQL子查询只能在select或where中出现   条款。

这有一个pending feature request

您必须重写查询以避免这种情况,或使用本机查询。