JPA多对多映射

多对多映射表示集合值关联, 其中任何数量的实体都可以与其他实体的集合关联。在关系数据库中, 一个实体的任何数量的行都可以称为另一实体的任何数量的行。
@ManyToMany示例 在此示例中, 我们将在学生与图书馆之间建立多对多关系, 以使任何数量的学生都能获得任何类型的书籍。
本示例包含以下步骤:-

  • 在com.srcmini.mapping包下创建一个包含学生ID(s_id)和学生姓名(s_name)的实体类Student.java, 并带有@ManyToMany批注, 该批注包含List类型的Library类对象。
import java.util.List; import javax.persistence.*; @Entitypublic class Student { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int s_id; private String s_name; @ManyToMany(targetEntity=Library.class) 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(); // TODO Auto-generated constructor stub } 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; } }

  • 在com.srcmini.mapping包下创建另一个实体类Library.java, 其中包含书ID(b_id), 书名(b_name)和带有@ManyToMany批注的书名(b_name), 其中批注包含List类型的Student类对象。
Library.java
package com.srcmini.mapping; import java.util.List; import javax.persistence.*; @Entitypublic class Library { @Id @GeneratedValue(strategy=GenerationType.AUTO)private int b_id; private String b_name; @ManyToMany(targetEntity=Student.class)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(); // TODO Auto-generated constructor stub}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文件中映射实体类和其他数据库配置。
Persistence.xml
< persistence> < persistence-unit name="books_issued"> < class> com.srcmini.mapping.Student< /class> < class> com.srcmini.mapping.Library< /class> < properties> < property name="javax.persistence.jdbc.driver" value="http://www.srcmini.com/com.mysql.jdbc.Driver"/> < property name="javax.persistence.jdbc.url" value="http://www.srcmini.com/jdbc:mysql://localhost:3306/mapping_db"/> < property name="javax.persistence.jdbc.user" value="http://www.srcmini.com/root"/> < property name="javax.persistence.jdbc.password" value=""/> < property name="eclipselink.logging.level" value="http://www.srcmini.com/SEVERE"/> < property name="eclipselink.ddl-generation" value="http://www.srcmini.com/create-or-extend-tables"/> < /properties> < /persistence-unit> < /persistence>

  • 在com.srcmini.ManyToOne包下创建一个持久性类ManyToOneExample以将实体对象与数据持久化。

package com.srcmini.mapping.ManyToMany; import java.util.ArrayList; import javax.persistence.*; import com.srcmini.mapping.Student; import com.srcmini.mapping.Library; 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, "Vipul", null); Student st2=new Student(2, "Vimal", 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, "Data Structure", al1); Library lib2=new Library(102, "DBMS", al2); em.persist(lib1); em.persist(lib2); em.getTransaction().commit(); em.close(); emf.close(); }}

输出:
【JPA多对多映射】程序执行后, 在MySQL工作台下生成了三个表。
  • 学生表-此表包含学生详细信息。要获取数据, 请在MySQL中从学生查询中运行select *。
JPA多对多映射

文章图片
  • 库表-此表包含库详细信息。要获取数据, 请在MySQL中从库查询中运行select *。
JPA多对多映射

文章图片
  • Library_student-此表包含库详细信息。要获取数据, 请在MySQL中运行select * from library_student查询。
JPA多对多映射

文章图片

    推荐阅读