【问题标题】:Convert sql query to hibernate criteria [closed]将sql查询转换为休眠条件[关闭]
【发布时间】: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 hibernate


    【解决方案1】:

    您发布的是本机 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();
    

    【讨论】:

    • 非常感谢...它对我有用...
    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2012-10-18
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    相关资源
    最近更新 更多