在JPQL中查找关系最多的实体

时间:2019-01-13 09:54:24

标签: java hibernate jpa

我有一个老师实体,每个学期都有很多人。

这么多的老师可以有很多学期。 我正在尝试编写一个JPQL查询,在那里我可以找到有最多学期的老师。

  public TeacherEntity teacherWithMostSemesters(){
    EntityManager em = emf.createEntityManager();
    em.createQuery("SELECT te from TeacherEntity te").getResultList();
    return null;
}

我的问题是我是根据数据库方案生成数据库的,

,它已经用FK为教师表和学期表创建了一个怪异的实体

package com.tvestergaard.start.data.entities;

import javax.persistence.*;
import java.util.Objects;

@Entity
@Table(name = "teacher_semester", schema = "student", catalog = "")
@IdClass(TeacherSemesterEntityPK.class)
public class TeacherSemesterEntity {
    private long teachingId;
    private long teachersId;

    @Id
    @Column(name = "teaching_ID")
    public long getTeachingId() {
        return teachingId;
    }

    public void setTeachingId(long teachingId) {
        this.teachingId = teachingId;
    }

    @Id
    @Column(name = "teachers_ID")
    public long getTeachersId() {
        return teachersId;
    }

    public void setTeachersId(long teachersId) {
        this.teachersId = teachersId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TeacherSemesterEntity that = (TeacherSemesterEntity) o;
        return teachingId == that.teachingId &&
                teachersId == that.teachersId;
    }

    @Override
    public int hashCode() {
        return Objects.hash(teachingId, teachersId);
    }
}

它应该已经在两者之间创建了一个关系,但是相反,它生成了带有两个外键的表的表...

我该如何解决这个问题,并进行JPQL查询,谁会找到所学课程最多的老师?

0 个答案:

没有答案