我有实体Group和Course。也有一个ManyToMany关系表。我想只获取该组的活动课程。
我如何在@WhereJoinTable()中设置一个条件,只过滤匹配的课程。Group.isActive = true, and semester_number = Group.currentSemester ?
还是有其他的方法来实现?
@Entity
@Table(name = "courses")
public class Course {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private String description;
@ManyToMany(mappedBy = "activeCourses")
private Set<Group> groups = new HashSet<>();
}
@Entity
@Table(name = "groups")
public class Group {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@Column(name = "current_semester")
private int currentSemester;
@Column(name = "total_semesters")
private int totalSemesters;
@Column(name = "is_active")
private Boolean isActive;
@Column(name = "department_id")
private long departmentId;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "assigned_courses", joinColumns = {@JoinColumn(name = "group_id")},
inverseJoinColumns = {@JoinColumn(name = "course_id")})
@WhereJoinTable(clause = "semester_number ="))
private Set<Course> activeCourses = new HashSet<>();
}
我通过在DAO层单独写一个查询来解决这个问题。看起来简单灵活多了。