Hibernate 如何在jpa中加载具有子实体计数的一对多关系实体

Hibernate 如何在jpa中加载具有子实体计数的一对多关系实体,hibernate,jpa,orm,toplink,Hibernate,Jpa,Orm,Toplink,我是在研究Spring/Hibernate/JPA时遇到这个问题的 这是我问题的概要 我在学生和出勤表之间有很多关系 以下是如何构建我的实体: @Entity class Student{ @Id private Long id; @OneToMany(mappedBy = "student", cascade = { CascadeType.PERSIST, CascadeType.MERGE, Cas

我是在研究Spring/Hibernate/JPA时遇到这个问题的

这是我问题的概要

我在学生和出勤表之间有很多关系

以下是如何构建我的实体:

@Entity
class Student{
@Id
private Long id;
    @OneToMany(mappedBy = "student",
               cascade = { CascadeType.PERSIST, CascadeType.MERGE,
                           CascadeType.REFRESH })
  private List<Attendance> attendances;
}

@Entity
class Attendance{

  @ManyToOne
  @JoinColumn(name = "id")
 private Student student;
}
上面的脚本是一个复杂的命名查询的一部分,还涉及其他实体,但我对将attendaces计数计入
N
的概念有点迷茫


非常感谢您提供的任何帮助。

您已将其标记为Hibernate,因此使用HQL您应该能够编写:

select student.id, count(student.attendances) from Student as student
select student.id, count(student.attendances) from Student as student