Я должен получить подробную информацию в зависимости от отдела и ниже условий. Я использую eclipseLink JPA 2.4+
if IS_SUBJECT = 2 Teacher entity should join with Subject
if IS_SUBJECT = 1 Teacher entity should join with Lab
, когда я получаю данные со значениями DepartmentId, которые должны отображаться в сущности Department и Teacher. JPA-запрос, как показано ниже.
SELECT distinct f FROM Department f " +
" join fetch f.teacherList fm" +
" join fetch fm.subject fmi on (f.isSubject = 2 and fm.subjectId = fmi.subjectId) " +
" join fetch fm.lab fmg on (f.isSubject = 1 and fm.labId = fmg.labId) " +
" WHERE f.departmentId IN :departmentIdList".
Объекты, как показано ниже:
@Entity
public class Department{
@Id
@Column(name = "DEPARTMENT_ID")
private Integer departmentId;
@OneToMany(mappedBy = "department")
private List<Teacher> teacherList;
@Column(name="IS_SUBJECT")
private isSubject;
}
@Entity
public class Teacher{
@Id
@Column(name = "TEACHER_ID")
private Integer teacherId;
@Column(name = "DEPARTMENT_ID")
private Integer departmentId;
@Column(name = "SUBJECT_ID")
private Integer subjectId;
@Column(name = "LAB_ID")
private Integer labId;
@ManyToOne
@JoinColumn(name = "DEPARTMENT_ID", insertable = false, updatable = false)
@BatchFetch(BatchFetchType.JOIN)
private Department department;
@JoinColumn(name = "SUBJECT_ID", insertable = false, updatable = false)
@BatchFetch(BatchFetchType.JOIN)
private Subject subject;
@BatchFetch(BatchFetchType.JOIN)
@JoinColumn(name = "LAB_ID", insertable = false, updatable = false)
private Lab lab;
}
@Entity
public class Subject{
@Id
@Column(name = "SUBJECT_ID")
private Integer subjectId;
@Column(name = "SUBJECT_NAME")
private Integer subjectName;
}
@Entity
public class Lab{
@Id
@Column(name = "LAB_ID")
private Integer labId;
@Column(name = "LAB_NAME")
private Integer labName;
}
Сгенерированный запрос занимает много времени для извлечения из ссылки затмения JPA. Пожалуйста, помогите мне в написании запроса.