一 代码位置
https://gitee.com/cakin24/code/tree/master/08/JpaOneToManyDemo
二 关键代码
1 School
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
@Entity
@Data
public class School {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
//@OneToMany(cascade = CascadeType.ALL)
@OneToMany()
@JoinColumn(name = "school_id")
private List<Teacher> teacherList;
}
2 Teacher
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.*;
@Data
@Entity
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToOne
private School school;
}
3 测试代码
package com.example.demo.entity;
import com.example.demo.repository.SchoolRepository;
import com.example.demo.repository.TeacherRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class OneToManyTest {
@Autowired
private SchoolRepository schoolRepository;
@Autowired
private TeacherRepository teacherRepository;
@Test
public void add() {
School school1 = new School();
school1.setName("清华大学");
schoolRepository.save(school1);
Teacher teacher = new Teacher();
teacher.setName("long");
teacher.setSchool(school1);
teacherRepository.save(teacher);
}
@Test
public void find() {
School school1 = new School();
school1 = schoolRepository.findSchoolById(1);
List<Teacher> teacherList = school1.getTeacherList();
System.out.println(school1.getName());
for (Teacher teacher : teacherList) {
System.out.println(teacher.getName());
}
}
@Test
public void deleteSchoolById() {
schoolRepository.deleteById(1);
}
@Test
public void deleteTeacherById() {
teacherRepository.deleteById(1);
}
}
三 测试
1 运行第一个Test
控制台输出:
Hibernate: create table school (id bigint not null auto_increment, name varchar(255), primary key (id)) engine=InnoDB
Hibernate: create table teacher (id bigint not null auto_increment, name varchar(255), school_id bigint, primary key (id)) engine=InnoDB
Hibernate: alter table teacher add constraint FKrg46bnmgbcccayv14naymqg3r foreign key (school_id) references school (id)
2020-01-02 10:30:52.397 INFO 796 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-01-02 10:30:53.222 INFO 796 --- [ main] com.example.demo.entity.OneToManyTest : Started OneToManyTest in 7.666 seconds (JVM running for 9.233)
Hibernate: insert into school (name) values (?)
Hibernate: insert into teacher (name, school_id) values (?, ?)
数据库如下
2 运行第二个Test
控制台输出:
Hibernate: select school0_.id as id1_0_, school0_.name as name2_0_ from school school0_ where school0_.id=?
清华大学
Hibernate: select teacherlis0_.school_id as school_i3_1_0_, teacherlis0_.id as id1_1_0_, teacherlis0_.id as id1_1_1_, teacherlis0_.name as name2_1_1_, teacherlis0_.school_id as school_i3_1_1_ from teacher teacherlis0_ where teacherlis0_.school_id=?
Hibernate: select school0_.id as id1_0_0_, school0_.name as name2_0_0_ from school school0_ where school0_.id=?
long
3 运行第三个Test
控制台输出:
Hibernate: select school0_.id as id1_0_0_, school0_.name as name2_0_0_ from school school0_ where school0_.id=?
Hibernate: update teacher set school_id=null where school_id=?
Hibernate: delete from school where id=?
数据库输出:
4 运行第四个Test
控制台输出:
Hibernate: select teacher0_.id as id1_1_0_, teacher0_.name as name2_1_0_, teacher0_.school_id as school_i3_1_0_, school1_.id as id1_0_1_, school1_.name as name2_0_1_ from teacher teacher0_ left outer join school school1_ on teacher0_.school_id=school1_.id where teacher0_.id=?
Hibernate: delete from teacher where id=?
数据库输出: