JPA实现“一对多”映射

一 代码位置

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=?

数据库输出:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值