【发布时间】:2016-01-03 19:24:01
【问题描述】:
我想将我的 sql 查询转换为休眠条件。请帮帮我。
这是我的 sql 查询:
select USER_ID, sum(TH_TOTAL_SCORE) as score from t_o2_user_gameplay
group by user_id order by score desc
【问题讨论】:
我想将我的 sql 查询转换为休眠条件。请帮帮我。
这是我的 sql 查询:
select USER_ID, sum(TH_TOTAL_SCORE) as score from t_o2_user_gameplay
group by user_id order by score desc
【问题讨论】:
您发布的是本机 SQL 查询。在您将其转换为使用 Criteria API 之前,我们需要先了解您的实体类及其属性。
这是一个示例:
@Entity
public class UserGamePlay {
private Long userId;
private Long totalScore;
...
}
HQL:
SELECT ugp.userId, SUM(ugp.totalScore)
FROM UserGamePlay ugp
GROUP BY ugp.userId
ORDER BY SUM(ugp.totalScore)
标准:
List results = session.createCriteria(UserGamePlay.class)
.setProjection( Projections.projectionList()
.add( Projections.property("userId"), "userId" )
.add( Projections.sum("totalScore"), "score" )
.add( Projections.groupProperty("userId"), "userId" )
)
.addOrder( Order.asc("score") )
.list();
【讨论】: