对象关系映射(object relational mapping)
映射方向是表与表的关联
单向关系:代表一个实体可以将属性引用到另一个实体。只能从A表向B表查询。
双向关关系代表每个实体都有一个关系字段引用了其他实体。
一对一 的映射
大概思路Student和Card两个实体,Student中包含了Card并使用@OneToOne注解。
在创建Student对象的时候,将cad值通过Student存入数据库中时候,删除的时候如果删除student,那么card也会被删除。如果是通过CardRepository存入数据库中,那么在删除student对象的时候,card仍然在数据库中保留。
hibernate运行日志:
1.png
测试类中的代码实现:
@Autowired
private StudentRepository studentRepository;
@Autowired
private CardRepository cardRepository;
@Test
public void testOneToOne(){
Student student1 = new Student();
student1.setName("张金金");
student1.setSex("male");
Student student2 = new Student();
student2.setName("孙非非");
student2.setSex("female");
Student student3 = new Student();
student3.setName("赵果果");
student3.setSex("female");
Card card1 = new Card();
card1.setNum(201203);
Card card2 = new Card();
card2.setNum(201204);
Card card3 = new Card();
card3.setNum(201205);
student1.setCard(card1);
student3.setCard(card3);
studentRepository.save(student1);
studentRepository.save(student2);
studentRepository.save(student3);
cardRepository.save(card2);
Long id1 = student1.getId();
studentRepository.deleteById(id1);
Long id2 = student2.getId();
studentRepository.deleteById(id2);
OneToOne完整代码拉取地址:
https://gitee.com/xgkp/SpringBoot8_7_1_one_to_one.git
一对一 的映射
Entity中的报错:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.demo.entity.School.teacherList, could not initialize proxy - no Session
解决方案:
OneToMore的One方添加注解
@OneToMany(fetch = FetchType.EAGER)
解决方案来源参看:https://www.baeldung.com/hibernate-initialize-proxy-exception
这里多备注一点:
在TestClass中,deleteSchoolById是先通过id找到对应数据Teacher.school_id,然后该条记录的外键置空,然后再删除School表的固定值
看打印的日志如下:
OneToMore的代码拉取地址:
https://gitee.com/xgkp/SpringBoot8_7_2_one_to_many.git
MoreToMore
多对多的关联关系中,只能通过中间表的方式进行映射,不能通过增加外键来实现。
Teacher实体类的核心代码实现
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "teacher_student",joinColumns = {@JoinColumn(name = "t_id")},inverseJoinColumns = {@JoinColumn(name = "s_id")})
private Set<Student> students;
Student实体类核心代码实现
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "teacher_student",joinColumns = {@JoinColumn(name = "s_id")},inverseJoinColumns = {@JoinColumn(name = "t_id")})
private Set<Teacher> teachers;
Respository代码实现
Student findById(long id);
Teacher findById(long id);
Service代码实现
public List<Student> getStudentList();
public Student findStudentById(long id);
ServiceImp代码实现
@Autowired
private StudentRepository studentRepository;
@Override
public List<Student> getStudentList() {
return studentRepository.findAll();
}
@Override
public Student findStudentById(long id) {
return studentRepository.findById(id);
}
运行测试的日志
1.png
数据库中的表结构
2.png
完整代码下载地址
https://gitee.com/xgkp/SpringBoot8_7_3_many_to_many.git
网友评论