简体   繁体   English

如何在不使用@Mapsid 的情况下使用 JPA Hibernate 将 3 个实体加入表中

[英]How can I join 3 entities in a table using JPA Hibernate without using @Mapsid

[this is my DB design](https://i.stack.imgur.com/7ckz4.png)
[Users entitie](https://i.stack.imgur.com/8mMfR.png)
[Roles entitie](https://i.stack.imgur.com/ACYNB.png)
[Project entitie](https://i.stack.imgur.com/ZJXKC.png)

The relation between Users and Has doesn't need to be unidirectional Users 和 Has 之间的关系不需要是单向的

Using this composite key class, we can create the entity class, which models the join table:使用这个复合键类,我们可以创建实体类,它模拟连接表:

@Entity
class CourseRating {

    @EmbeddedId
    CourseRatingKey id;

    @ManyToOne
    @MapsId("studentId")
    @JoinColumn(name = "student_id")
    Student student;

    @ManyToOne
    @MapsId("courseId")
    @JoinColumn(name = "course_id")
    Course course;

    int rating;
    
    // standard constructors, getters, and setters
}


This code is very similar to a regular entity implementation. However, we have some key differences:

We used @EmbeddedId to mark the primary key, which is an instance of the CourseRatingKey class.
We marked the student and course fields with @MapsId.
@MapsId means that we tie those fields to a part of the key, and they're the foreign keys of a many-to-one relationship. We need it because, as we mentioned, we can't have entities in the composite key.

After this, we can configure the inverse references in the Student and Course entities as before:

class Student {

    // ...

    @OneToMany(mappedBy = "student")
    Set<CourseRating> ratings;

    // ...
}

class Course {

    // ...

    @OneToMany(mappedBy = "course")
    Set<CourseRating> ratings;

    // ...
}

Note that there's an alternative way to use composite keys: the @IdClass annotation.请注意,还有一种使用复合键的替代方法:@IdClass 注释。

3.4. 3.4. Further Characteristics We configured the relationships to the Student and Course classes as @ManyToOne.更多特征 我们将与 Student 和 Course 类的关系配置为 @ManyToOne。 We could do this because with the new entity we structurally decomposed the many-to-many relationship to two many-to-one relationships.我们可以这样做是因为使用新实体,我们在结构上将多对多关系分解为两个多对一关系。

Why were we able to do this?为什么我们能够做到这一点? If we inspect the tables closely in the previous case, we can see that it contained two many-to-one relationships.如果我们仔细检查前一个案例中的表,我们可以看到它包含两个多对一关系。 In other words, there isn't any many-to-many relationship in an RDBMS.换句话说,RDBMS 中没有任何多对多关系。 We call the structures we create with join tables many-to-many relationships because that's what we model.我们将使用连接表创建的结构称为多对多关系,因为这就是我们建模的对象。

Besides, it's more clear if we talk about many-to-many relationships because that's our intention.此外,如果我们谈论多对多关系会更清楚,因为这是我们的意图。 Meanwhile, a join table is just an implementation detail;同时,连接表只是一个实现细节; we don't really care about it.我们并不真正关心它。

Moreover, this solution has an additional feature we haven't mentioned yet.此外,此解决方案还有一个我们尚未提及的附加功能。 The simple many-to-many solution creates a relationship between two entities.简单的多对多解决方案在两个实体之间创建关系。 Therefore, we cannot expand the relationship to more entities.因此,我们无法将关系扩展到更多实体。 But we don't have this limit in this solution: we can model relationships between any number of entity types.但是我们在这个解决方案中没有这个限制:我们可以对任意数量的实体类型之间的关系进行建模。

For example, when multiple teachers can teach a course, students can rate how a specific teacher teaches a specific course.例如,当多个教师可以教授一门课程时,学生可以评价特定教师教授特定课程的方式。 That way, a rating would be a relationship between three entities: a student, a course and a teacher.这样一来,评级将成为三个实体之间的关系:学生、课程和教师。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用带有@MapsId 的@OneToOne 映射时强制hibernate 加入 - How to force hibernate to do join while using @OneToOne mapping with @MapsId 如何使用 JPA 和 Hibernate 加入两个不相关的实体 - How to join two unrelated entities using JPA and Hibernate 如何使用Hibernate Envers审核连接表和相关实体? - How not to audit a join table and related entities using Hibernate Envers? 如何使用 Hibernate &amp; Spring Data JPA 正确注释两个实体之间的这种关系? - How can I properly annotate this relationship between two entities using Hibernate & Spring Data JPA? 如何使用 Spring JPA/Hibernate 实现 Join With Temporary Table - How to achieve Join With Temporary Table using Spring JPA/Hibernate 使用@MapsId 时,由于 SQL INSERT 中缺少 JPA OneToOne 关系 FK,Hibernate 抛出“列不允许为 NULL” - Hibernate throws "NULL not allowed for column" as the JPA OneToOne relation FK is missing in the SQL INSERT when using @MapsId 如何在两个实体(不相关的实体)之间使用Hibernate进行连接 - How to Do a Join using Hibernate between two Entities (unrelated Entities) 如何使用@ManyToMany和hibernate在实体之间创建中间表? - How can I create an intermediate table between to entities using @ManyToMany with hibernate? 如何映射列表<String>使用 Hibernate/JPA 而没有额外的连接表? - How to map a List<String> with Hibernate/JPA WITHOUT an extra join table? 如何使用Hibernate或JPA动态生成实体集合? - How can i generate dynamically collections of entities with hibernate or jpa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM