我阅读了有关如何使用组合键构建多对多的this article。这些实体是
class Student {
// ...
@OneToMany(mappedBy = "student")
Set<CourseRating> ratings;
// ...
}
class Course {
// ...
@OneToMany(mappedBy = "course")
Set<CourseRating> ratings;
// ...
}
@Entity
class CourseRating {
@EmbeddedId
CourseRatingKey id;
@ManyToOne
@MapsId("student_id")
@JoinColumn(name = "student_id")
Student student;
@ManyToOne
@MapsId("course_id")
@JoinColumn(name = "course_id")
Course course;
int rating;
// standard constructors, getters, and setters
}
@Embeddable
class CourseRatingKey implements Serializable {
@Column(name = "student_id")
Long studentId;
@Column(name = "course_id")
Long courseId;
// standard constructors, getters, and setters
// hashcode and equals implementation
}
现在,让我们假设,我想要得到具有特定courseId的学生。
那么我的JPQ看起来会是什么样子:
select s from Student s join fetch s.ratings r where r.id.courseId=:courserId
或
select s from Student s join fetch s.ratings r where r.course.id=:courserId
或者它将是完全不同的?
https://stackoverflow.com/questions/56760483
复制相似问题