JPA之OneToMany、ManyToOne示例


3个月前 166次点击 来自 后端

标签: JPA

四个实体类:学生, 教室,老师,课桌

  • 一个学生通常只有一个课桌,一个课桌通常给一个学生作,这里学生和课桌的关系就是互为 @OneToOne
  • 一个教室通常可以容纳很多的学生,教室到学生的关系就可以定义为 @OneToMany
  • 很多学生容纳在一个教室当中,学生到教室的关系可以定义为 @ManyToOne
  • 一个学生可以有很多的老师,一个老师可以有很多的学生,这里学生和老师的关系就互为 @ManyToMany

一对多关系中,我们把一的一方称之为主表,把多的一方称之为从表。在数据库中建立一对多的关系,需要使用数据库的外键约束。

OneToMany

Classroom.java

@Entity
@Setter
@Getter
@Table(name = "t_classroom")
public class Classroom {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    /**
     * 使用 @OneToMany 的时候通常会紧跟 mappedBy 属性,一方通常是不需要维护主键的,主键在 @ManyToOne 的一方, 当然也可以设置中间表来配置外键
     */
    @OneToMany(mappedBy = "classroom", cascade = {CascadeType.PERSIST})
    private Set<Student> students;
}

ManyToOne

Student.java 添加一下属性:

/**
 * 一对多和多对一的关系维护中,通常在多的一方进行外键的维护,运行程序我们会发现在 stu 表中新增了一个 classroom_id 的外键
 */
@ManyToOne
private Classroom classroom;

测试

@Test
void testSave() {
    Classroom classroom1 = new Classroom();
    classroom1.setName("一年一班");
    classroomRepository.save(classroom1);
}

@Test
@Transactional
@Rollback(false)
void testUpdate() {
    Classroom classroom = classroomRepository.getById(1L);
    Student student = studentRepository.getById(1L);
			//注意:教室使用了 mappedBy 属性放弃了主键的维护,因此我们需要借助学生类来维护彼此的关系
    student.setClassroom(classroom);
    student.setName("大力水手 - 1");
    classroom.setName("一年一班 - 1");
    classroom.getStudents().add(student);
    classroomRepository.save(classroom);
}

SQL

Hibernate: select student0_.id as id1_28_0_, student0_.age as age2_28_0_, student0_.classroom_id as classroo4_28_0_, student0_.name as name3_28_0_, student0_1_.desk_id as desk_id1_29_0_, classroom1_.id as id1_26_1_, classroom1_.name as name2_26_1_, desk2_.id as id1_27_2_, desk2_.number as number2_27_2_, desk2_1_.stu_id as stu_id0_29_2_, student3_.id as id1_28_3_, student3_.age as age2_28_3_, student3_.classroom_id as classroo4_28_3_, student3_.name as name3_28_3_, student3_1_.desk_id as desk_id1_29_3_ from t_stu student0_ left outer join t_stu_desk student0_1_ on student0_.id=student0_1_.stu_id left outer join t_classroom classroom1_ on student0_.classroom_id=classroom1_.id left outer join t_desk desk2_ on student0_1_.desk_id=desk2_.id left outer join t_stu_desk desk2_1_ on desk2_.id=desk2_1_.desk_id left outer join t_stu student3_ on desk2_1_.stu_id=student3_.id left outer join t_stu_desk student3_1_ on student3_.id=student3_1_.stu_id where student0_.id=?
Hibernate: select students0_.classroom_id as classroo4_28_0_, students0_.id as id1_28_0_, students0_.id as id1_28_1_, students0_.age as age2_28_1_, students0_.classroom_id as classroo4_28_1_, students0_.name as name3_28_1_, students0_1_.desk_id as desk_id1_29_1_, desk1_.id as id1_27_2_, desk1_.number as number2_27_2_, desk1_1_.stu_id as stu_id0_29_2_, student2_.id as id1_28_3_, student2_.age as age2_28_3_, student2_.classroom_id as classroo4_28_3_, student2_.name as name3_28_3_, student2_1_.desk_id as desk_id1_29_3_ from t_stu students0_ left outer join t_stu_desk students0_1_ on students0_.id=students0_1_.stu_id left outer join t_desk desk1_ on students0_1_.desk_id=desk1_.id left outer join t_stu_desk desk1_1_ on desk1_.id=desk1_1_.desk_id left outer join t_stu student2_ on desk1_1_.stu_id=student2_.id left outer join t_stu_desk student2_1_ on student2_.id=student2_1_.stu_id where students0_.classroom_id=?
Hibernate: update t_stu set age=?, classroom_id=?, name=? where id=?
Hibernate: update t_classroom set name=? where id=?

Made with in Shangrao,China By Devler.

Copyright © Devler 2012 - 2022

赣ICP备19009883号-1

Top ↑