将sql查询转换为休眠标准

时间:2015-10-06 18:45:50

标签: sql hibernate

我想将我的sql查询转换为休眠标准。请帮帮我。

这是我的SQL查询:

 select USER_ID, sum(TH_TOTAL_SCORE) as score from t_o2_user_gameplay
 group by user_id order by score desc  

1 个答案:

答案 0 :(得分:1)

您发布的是本机SQL查询。在将其转换为使用Criteria API之前,我们首先需要了解您的Entity类及其属性。

以下是一个示例:

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