欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jpa之一对多关系

程序员文章站 2022-07-09 11:50:35
...

jpa之一对多关系

单向一对多

单向一对多只需要在一方加@OneToMany,而多方则什么都不用添加

DepartMentDo.java:

@Entity
@Table(name = "t_depart")
public class DepartMentDo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(targetEntity = EmployeeDo.class, fetch = FetchType.LAZY)
    private Collection employees;
    //getter and setter
}

@Entity
@Table(name = "t_emp")
public class EmployeeDo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
}

生成表t_depart,t_emp和关系维护表t_depart_employees,关系维护表由``一方表名[email protected]`注解上属性名称组成

mysql> desc t_depart;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set

mysql> desc t_emp;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set

mysql> desc t_depart_employees;
+-------------------+---------+------+-----+---------+-------+
| Field             | Type    | Null | Key | Default | Extra |
+-------------------+---------+------+-----+---------+-------+
| depart_ment_do_id | int(11) | NO   | MUL | NULL    |       |
| employees_id      | int(11) | NO   | PRI | NULL    |       |
+-------------------+---------+------+-----+---------+-------+
2 rows in set

双向一对多

多的一方就是关系的拥有者即有@JoinColumn的一方,mappedBy指的是关系的反方,值的话一般是关系拥有者中的属性(field),即EmployeeDo实体中的departMentDo属性字段对应的值

DepartMentDo.java:

@Entity
@Table(name = "t_depart")
public class DepartMentDo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @OneToMany(targetEntity = EmployeeDo.class, fetch = FetchType.LAZY, mappedBy = "departMentDo")
    private Collection employee;

    //getter and setter
}

EmployeeDo.java:

@Entity
@Table(name = "t_emp")
public class EmployeeDo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;

    @ManyToOne
    @JoinColumn(name = "dept_id")
    private DepartMentDo departMentDo;

    //getter and setter
}

生成的表:

mysql> desc t_depart;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
2 rows in set

mysql> desc t_emp;
+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| id      | int(11)      | NO   | PRI | NULL    | auto_increment |
| name    | varchar(255) | YES  |     | NULL    |                |
| dept_id | int(11)      | YES  | MUL | NULL    |                |
+---------+--------------+------+-----+---------+----------------+