Hibernate标准内部连接来选择 [英] Hibernate criteria Inner join to select

查看:100
本文介绍了Hibernate标准内部连接来选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将下一个表达式转换为hibernate标准api:

  select * from games gs 
INNER JOIN (从gg
中选择gg.ID,gg.CODE,max(gg.DATE)max_date,其中gg.player_id = 1组由gg.ID,gg.CODE)ss
on gs.CODE = ss.CODE
和gs.ID = ss.ID
和gs.DATE = ss.max_date
和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不支持<$ c $中的子查询c>来自子句。从 Hibernate 4.3文档:
$ b


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


有一个待处理功能请求为此。



您必须重写查询才能避免这种情况,或者使用原生查询。


I have to translate the next expression into 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

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. From the Hibernate 4.3 documentation:

Note that HQL subqueries can occur only in the select or where clauses.

There is a pending feature request for this.

You'll have to rewrite the query to avoid that, or use native queries.

这篇关于Hibernate标准内部连接来选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆