【问题标题】:hibernate extra column with custom query(subquery)使用自定义查询(子查询)休眠额外列
【发布时间】:2017-03-09 23:17:35
【问题描述】:
@Entity
@Table(name="student")
public class Student
    @Id
    private Integer id;
    private String name;
    private Integer count;

我想分配给

计数

count = SELECT COUNT(*) FROM people

如何使用 JpaRepository 将 result:SELECT COUNT(*) FROM people 分配给整数?

【问题讨论】:

    标签: java mysql hibernate spring-data-jpa


    【解决方案1】:

    @Formula 帮助您在Hibernate 中创建自己的公式。

    @Entity
    @Table(name="student")
    public class Student
        @Id
        private Integer id;
        private String name;
        @Formula("(SELECT COUNT(*) FROM people)")  // people is table name not entity
        private Integer count;
    

    【讨论】:

    • 是否可以在公式中添加条件?比如:@Formula("(SELECT COUNT(*) FROM people where group = :groupId)")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 2011-04-13
    相关资源
    最近更新 更多