首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JPQL:使用组合键建模的具有多对多关系的查询实体

JPQL:使用组合键建模的具有多对多关系的查询实体
EN

Stack Overflow用户
提问于 2019-06-26 03:00:23
回答 1查看 159关注 0票数 1

我阅读了有关如何使用组合键构建多对多的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

或者它将是完全不同的?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56760483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档