博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JPA--多对多关系
阅读量:6703 次
发布时间:2019-06-25

本文共 6873 字,大约阅读时间需要 22 分钟。

hot3.png

JPA中,多对多关系用@ManyToMany标示。

关系维护端:

1 package com.yl.demo1.bean.manyTomany; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.GeneratedValue;10 import javax.persistence.Id;11 import javax.persistence.JoinColumn;12 import javax.persistence.JoinTable;13 import javax.persistence.ManyToMany;14 15 @Entity16 public class Student {17     private Integer id;18     private String name;19     private Set
teachers = new HashSet
();20 21 22 public Student(){}23 24 public Student(String name) {25 super();26 this.name = name;27 }28 @Id @GeneratedValue29 public Integer getId() {30 return id;31 }32 public void setId(Integer id) {33 this.id = id;34 }35 @Column(length=10, nullable=false)36 public String getName() {37 return name;38 }39 public void setName(String name) {40 this.name = name;41 }42 /**43 * Student是关系的维护端44 * inverseJoinColumns和joinColumns是定义关联表中的字段,其中inverseJoinColumns定义关系被维护端的字段45 * joinColumns定义关系维护端的字段46 */47 @ManyToMany(cascade=CascadeType.REFRESH)48 @JoinTable(name="student_teacher", inverseJoinColumns=@JoinColumn(name="teacher_id"),49 joinColumns=@JoinColumn(name="student_id"))50 public Set
getTeachers() {51 return teachers;52 }53 public void setTeachers(Set
teachers) {54 this.teachers = teachers;55 }56 57 public void addTeacher(Teacher teacher) {58 this.teachers.add(teacher);59 }60 61 public void removeTeacher(Teacher teacher) {62 if (this.teachers.contains(teacher)) {63 this.teachers.remove(teacher);64 }65 }66 }

 

关系被维护端:

1 package com.yl.demo1.bean.manyTomany; 2  3 import java.util.HashSet; 4 import java.util.Set; 5  6 import javax.persistence.CascadeType; 7 import javax.persistence.Column; 8 import javax.persistence.Entity; 9 import javax.persistence.FetchType;10 import javax.persistence.GeneratedValue;11 import javax.persistence.Id;12 import javax.persistence.ManyToMany;13 14 @Entity15 public class Teacher {16     private Integer id;17     private String name;18     private Set
students = new HashSet
();19 20 public Teacher(){}21 22 public Teacher(String name) {23 super();24 this.name = name;25 }26 @Id @GeneratedValue27 public Integer getId() {28 return id;29 }30 public void setId(Integer id) {31 this.id = id;32 }33 @Column(length=10, nullable=false)34 public String getName() {35 return name;36 }37 public void setName(String name) {38 this.name = name;39 }40 //teacher为关系的被维护端41 @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="teachers", fetch=FetchType.LAZY)42 public Set
getStudents() {43 return students;44 }45 public void setStudents(Set
students) {46 this.students = students;47 }48 49 @Override50 public int hashCode() {51 final int prime = 31;52 int result = 1;53 result = prime * result + ((id == null) ? 0 : id.hashCode());54 return result;55 }56 57 @Override58 public boolean equals(Object obj) {59 if (this == obj)60 return true;61 if (obj == null)62 return false;63 if (getClass() != obj.getClass())64 return false;65 Teacher other = (Teacher) obj;66 if (id == null) {67 if (other.id != null)68 return false;69 } else if (!id.equals(other.id))70 return false;71 return true;72 }73 74 75 76 77 }

常见操作:

1 @Test 2     public void save() { 3         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL"); 4         EntityManager em = factory.createEntityManager(); 5         em.getTransaction().begin();//事务开始 6          7         Student student = new Student("yyyy"); 8         em.persist(student); 9         10         Teacher teacher = new Teacher("jiawenhui");11         em.persist(teacher);12         13         em.getTransaction().commit();14         em.close();15         factory.close();16     }17 18     /**19      * 建立学生与老师之间的关系20      */21     @Test22     public void buildConnection() {23         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");24         EntityManager em = factory.createEntityManager();25         em.getTransaction().begin();//事务开始26         27         Student student = em.find(Student.class, 1);28         29         Teacher teacher = em.getReference(Teacher.class, 1);30         31         student.addTeacher(teacher);32         33         em.getTransaction().commit();34         em.close();35         factory.close();36     }37 38     /**39      * 解除学生与老师之间的关系40      */41     @Test42     public void deleteConnection() {43         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");44         EntityManager em = factory.createEntityManager();45         em.getTransaction().begin();//事务开始46         47         Student student = em.find(Student.class, 1);48         49         Teacher teacher = em.getReference(Teacher.class, 1);50         51         student.removeTeacher(teacher);52         53         em.getTransaction().commit();54         em.close();55         factory.close();56     }57     58     /**59      * 删除老师(删除关系被维护端)60      */61     @Test62     public void deleteTeacher() {63         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");64         EntityManager em = factory.createEntityManager();65         em.getTransaction().begin();//事务开始66         67         Student student = em.find(Student.class, 1);68         Teacher teacher = em.getReference(Teacher.class, 1);69         //先解除Student和Teacher之间的关系70         student.removeTeacher(teacher);71         //在删除Teacher72         em.remove(teacher);73         74         em.getTransaction().commit();75         em.close();76         factory.close();77     }78     79     /**80      * 删除学生(删除关系维护端)81      */82     @Test83     public void deleteStudent() {84         EntityManagerFactory factory = Persistence.createEntityManagerFactory("YL");85         EntityManager em = factory.createEntityManager();86         em.getTransaction().begin();//事务开始87         88         Student student = em.getReference(Student.class, 1);89         em.remove(student);90         91         em.getTransaction().commit();92         em.close();93         factory.close();94     }95

 

转载于:https://my.oschina.net/yulei1943/blog/1490686

你可能感兴趣的文章
Python基础-变量作用域
查看>>
如何删除一个标签,但是保留里面的内容?
查看>>
支持取消操作和暂停操作的Backgroundworker
查看>>
扯扯所学的编程语言
查看>>
POJ 3358 Period of an Infinite Binary Expansion
查看>>
PHP——上传头像(2)
查看>>
Silverlight企业应用框架设计【六】自定义系统菜单(使用自己的DataForm)
查看>>
在线购物系统权限模块
查看>>
Asp.net和C# 函数方法 (2)【转载】
查看>>
wget用法详解
查看>>
session监听
查看>>
android 拍照注意问题
查看>>
WIN8系统中 任务管理器 性能栏 显示CPU利用率(已暂停)怎么回事?
查看>>
whu oj 1551 Pairs (莫队算法)
查看>>
iOS:iOS开发系列–打造自己的“美图秀秀”(中)
查看>>
程序猿的还有一出路:大数据project师
查看>>
FIREDAC直连ORACLE数据库
查看>>
Gac代码库分析(3)智能指针
查看>>
【转】MFC 字体LOGFONT
查看>>
iOS 图片填充 UIImageView
查看>>