SpringBoot与Hibernate整合 一对多,多对多(实例)

简介

一对多:

在一对多双向关联时,在一的一方需要设置@OneToMany和mappedby,在多的一方需要设置@ManyToOne和@JoinColumn

多对多:

在多对多双向关联时,在控制的一方设置@ManyToMany和mappedby,在另一方需要设置@ManyToMany和@JoinTable

Demo目录

SpringBoot与Hibernate整合 一对多,多对多(实例)_第1张图片

数据表

SpringBoot与Hibernate整合 一对多,多对多(实例)_第2张图片

核心代码

application.properties
#自己的数据库,hibernate可以创建表,但不能创库
spring.datasource.url=jdbc:mysql://localhost:3306/hibernate?&characterEncoding=utf8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=password
#create 删表后再创表,要丢数据,update 只能增加表或字段。我事先是没有数据表,只建了对象
spring.jpa.hibernate.ddl-auto=create
#是否在控制台上输出SQL语句
spring.jpa.show-sql=true
Student.java
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "student_id")
    private int id;
    @Column(name = "student_name")
    private String name;
    @Column(name = "student_password")
    private String password;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "school")
    private School school;
    @ManyToMany(cascade = CascadeType.ALL,mappedBy = "students")//mappedBy=“关联属性名”
    private Set teachers;

    public Student() {
    }

    public Student(String name, String password, School school, Set teachers) {
        this.name = name;
        this.password = password;
        this.school = school;
        this.teachers = teachers;
    }
    //Get and Set...
}
Teacher.java
@Entity
@Table(name = "teacher")
public class Teacher {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "teacher_id")
    private int id;
    @Column(name = "teacher_name")
    private String name;
    @Column(name = "teacher_password")
    private String password;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "school")
    private School school;
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "teacher_student",//中间表名
    joinColumns = {@JoinColumn(name = "teacherid")},//当前对象id在中间表的列名
    inverseJoinColumns = {@JoinColumn(name = "studentid")}//另一张表在中间表的列名
    )
    private Set students;

    public Teacher() {
    }

    public Teacher(String name, String password, School school, Set students) {
        this.name = name;
        this.password = password;
        this.school = school;
        this.students = students;
    }
    //Get and Set...
}
School.java
@Entity
public class School {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "school_id")
    private int id;
    @Column(name = "school_name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL,mappedBy = "school")
    private Set students;
    @OneToMany(cascade = CascadeType.ALL,mappedBy = "school")
    private Set teachers;

    public School() {
    }

    public School(String name, Set students, Set teachers) {
        this.name = name;
        this.students = students;
        this.teachers = teachers;
    }
    //Get and Set...

运行结果

SpringBoot与Hibernate整合 一对多,多对多(实例)_第3张图片
POST之后
SpringBoot与Hibernate整合 一对多,多对多(实例)_第4张图片

你可能感兴趣的:(Java开发)