I have a teacher entity, which has a ManytoMany with semesters.
So many teachers can have many semesters. I'm trying to write a JPQL query, where I can get the teacher, that has the most amount of semesters.
public TeacherEntity teacherWithMostSemesters(){
EntityManager em = emf.createEntityManager();
em.createQuery("SELECT te from TeacherEntity te").getResultList();
return null;
}
My issue is that I have generated my database from a database scheme,
and it has created a weird entity with the FK for both the teacher's table and the semester table
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);
}
}
It should have created a relationship between the two, but instead, it generated a table of the table with the two foreign keys...
How can I fix this, and make a JPQL query, who finds the teacher with the most semesters taught?