使用自定义查询(子查询)来休眠额外列

时间:2016-10-27 10:04:51

标签: java mysql hibernate spring-data-jpa

@Entity
@Table(name="student")
public class Student
    @Id
    private Integer id;
    private String name;
    private Integer count;

我想分配给

  

计数

count = SELECT COUNT(*) FROM people

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

1 个答案:

答案 0 :(得分:5)

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