JPA(Java Persistence API)学习十二(多对多映射)
1.概述
多对多映射表示集合值关联,其中任何数量的实体可以与其他实体的集合关联。
在关系数据库中,一个实体的任何行可以被引用到另一个实体的任意数量的行。
2.@ManyToMany 示例
第一步:创建一个实体类Student.java
,包含学生ID(s_id)和学生姓名(s_name),
其中包含一个使用@@ManyToMany
注解的List
类型的Library
类对象。
代码:
import java.util.List;
import javax.persistence.*;
@Entity
public class Student {
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int s_id;
private String s_name;
@GeneratedValue(strategy = GenerationType.AUTO)
private int s_id;
private String s_name;
@ManyToMany(targetEntity = Library.class)
private List lib;
}
private List lib;
}
第二步:在包含
book id(b_id)
,book name(b_name)
和@ManyToMany
注释的 实体类
Library.java
,其中包含List类型的Student
类对象。 代码:
import java.util.List;
import javax.persistence.*;
@Entity
public class Library {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int b_id;
private String b_name;
public class Library {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int b_id;
private String b_name;
@ManyToMany(targetEntity = Student.class)
private List stud;
private List stud;
}
第三步:将实体类和数据库配置映射到
persistence.xml
文件中 <?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="book_issued">
<class>com.yiibai.mapping.Student</class>
<class>com.yiibai.mapping.Library</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="book_issued">
<class>com.yiibai.mapping.Student</class>
<class>com.yiibai.mapping.Library</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
3.应用
EntityManagerFactory emf = Persistence.createEntityManagerFactory("books_issued");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Student st1 = new Student(1, "Maxsu", null);
Student st2 = new Student(2, "Xiaonew", null);
Student st2 = new Student(2, "Xiaonew", null);
em.persist(st1);
em.persist(st2);
em.persist(st2);
ArrayList<Student> al1 = new ArrayList<Student>();
ArrayList<Student> al2 = new ArrayList<Student>();
ArrayList<Student> al2 = new ArrayList<Student>();
al1.add(st1);
al1.add(st2);
al1.add(st2);
al2.add(st1);
al2.add(st2);
al2.add(st2);
Library lib1 = new Library(101, "Data Structure", al1);
Library lib2 = new Library(102, "DBMS", al2);
Library lib2 = new Library(102, "DBMS", al2);
em.persist(lib1);
em.persist(lib2);
em.persist(lib2);
em.getTransaction().commit();
em.close();
emf.close();
em.close();
emf.close();
学习来源:https://www.yiibai.com/jpa/jpa-many-to-many-mapping.html#article-start