Hibernate 标准内部加入 select
Hibernate criteria Inner join to select
我必须将下一个表达式转换为休眠条件 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();
Hibernate 不支持 from
子句中的子查询。来自 Hibernate 4.3 documentation:
Note that HQL subqueries can occur only in the select or where
clauses.
为此有一个pending feature request。
您必须重写查询以避免这种情况,或者使用本机查询。
我必须将下一个表达式转换为休眠条件 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();
Hibernate 不支持 from
子句中的子查询。来自 Hibernate 4.3 documentation:
Note that HQL subqueries can occur only in the select or where clauses.
为此有一个pending feature request。
您必须重写查询以避免这种情况,或者使用本机查询。