JPA 多对多映射
多对多映射表示集合值关联,其中任意数量的实体可以与其他实体的集合相关联。在关系数据库中,一个实体的任意数量的行都可以引用另一个实体的任意数量的行。
JPA @ManyToMany多对多映射示例
项目结构如下:
在此示例中,我们将在学生和图书馆之间创建多对多关系,以便可以向任意数量的学生分发任何类型的书籍。
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yiidian</groupId>
<artifactId>JPAProject</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
</project>
Student实体类
package com.yiidian.pojo;
import javax.persistence.*;
import java.util.List;
@Entity
public class Student {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int s_id;
private String s_name;
@ManyToMany(targetEntity=Library.class)
@JoinTable(name = "library_student",joinColumns = @JoinColumn(name = "student_id"),inverseJoinColumns = @JoinColumn(name = "library_id"))
private List lib;
public Student(int s_id, String s_name, List lib) {
super();
this.s_id = s_id;
this.s_name = s_name;
this.lib = lib;
}
public Student() {
super();
}
public List getLib() {
return lib;
}
public void ListLib(List lib) {
this.lib = lib;
}
public int getS_id() {
return s_id;
}
public void ListS_id(int s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void ListS_name(String s_name) {
this.s_name = s_name;
}
}
Library实体类
package com.yiidian.pojo;
import javax.persistence.*;
import java.util.List;
@Entity
public class Library {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int b_id;
private String b_name;
@ManyToMany(targetEntity=Student.class)
@JoinTable(name = "library_student",joinColumns = @JoinColumn(name = "library_id"),inverseJoinColumns = @JoinColumn(name = "student_id"))
private List stud;
public Library(int b_id, String b_name, List stud) {
super();
this.b_id = b_id;
this.b_name = b_name;
this.stud = stud;
}
public Library() {
super();
}
public int getB_id() {
return b_id;
}
public void ListB_id(int b_id) {
this.b_id = b_id;
}
public String getB_name() {
return b_name;
}
public void ListB_name(String b_name) {
this.b_name = b_name;
}
public List getStud() {
return stud;
}
public void ListStud(List stud) {
this.stud = stud;
}
}
persistence.xml
在resources目录下创建META-INF目录,建立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="books_issued">
<class>com.yiidian.pojo.Student</class>
<class>com.yiidian.pojo.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/jpa?serverTimezone=UTC&useSSL=false" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
ManyToManyExample测试类
package com.yiidian.test;
import com.yiidian.pojo.Library;
import com.yiidian.pojo.Student;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.ArrayList;
public class ManyToManyExample {
public static void main(String[] args) {
EntityManagerFactory emf= Persistence.createEntityManagerFactory("books_issued");
EntityManager em=emf.createEntityManager();
em.getTransaction().begin();
Student st1=new Student(1,"Jack",null);
Student st2=new Student(2,"Rose",null);
em.persist(st1);
em.persist(st2);
ArrayList<Student> al1=new ArrayList<Student>();
ArrayList<Student> al2=new ArrayList<Student>();
al1.add(st1);
al1.add(st2);
al2.add(st1);
al2.add(st2);
Library lib1=new Library(101,"数据结构",al1);
Library lib2=new Library(102,"MySQL入门",al2);
em.persist(lib1);
em.persist(lib2);
em.getTransaction().commit();
em.close();
emf.close();
}
}
程序执行后,在MySQL下生成了三个表。
- student表 : 此表包含学生详细信息。要获取数据,请在 MySQL 中运行select * from student查询
- library表 : 此表包含图书馆的详细信息。要获取数据,请在 MySQL 中运行select * from library查询
- library_student : 此表包含图书馆的详细信息。要获取数据,请在 MySQL 中运行select * from library_student查询
热门文章
优秀文章