JPA 多对多(Many-to-Many)是一种常见的数据库关系,它表示两个实体之间存在多对多的关系。在 JPA 中,可以使用 @ManyToMany 注解来映射多对多的关系。
@Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToMany(mappedBy = "students") private Listcourses; // getters and setters ... }
上面的代码段中,我们使用了 @ManyToMany 注解来映射 Student 和 Course 之间的多对多关系。@ManyToMany 注解有一个 mappedBy 属性,它指向 Course 类中 students 属性。这样,JPA 就可以根据这个属性来识别出 Student 和 Course 之间的关系。
@Entity public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) // 这里加上 cascade 级联操作属性 @JoinTable(name = "student_course", joinColumns = {@JoinColumn(name = "course_id")}, inverseJoinColumns = {@JoinColumn(name = "student_id")}) // 这里加上 JoinTable 来映射中间表 private Liststudents; // getters and setters ... }
在上面的代码段中,我们使用了 cascade 属性来声明当保存、更新、删除 Course 实体时会同时保存、更新、删除相应的 Student 实体。此外,我们还使用了 JoinTable 来映射中间表 student_course (即 student 表和 course 表之间的关联表)。
以下代码显示如何为多对多映射设置连接表。
下面的代码来自PersonDaoImpl.java。
package cn..common; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.springframework.transaction.annotation.Transactional; @Transactional public class PersonDaoImpl { public void test(){ Department d1 = new Department(); d1.setName("Design"); Department d2 = new Department(); d2.setName("Testing"); Person p1 = new Person("Tom"); p1.getDepartments().add(d1); p1.getDepartments().add(d2); Person p2 = new Person("Jack"); p2.getDepartments().add(d1); p2.getDepartments().add(d2); d1.getPersons().add(p1); d1.getPersons().add(p2); em.persist(p1); em.persist(p2); em.persist(d1); em.persist(d2); } @PersistenceContext private EntityManager em; }
下面的代码来自Person.java。
package cn..common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; @Entity public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToMany @JoinTable(name="PERSON_DEPT", joinColumns=@JoinColumn(name="PERSON_ID"), inverseJoinColumns=@JoinColumn(name="DEPT_ID")) private Set<Department> departments = new HashSet<Department>(); public Person() {} public Person(String name) { this.name = name; } public Set<Department> getDepartments() { return departments; } public void setDepartments(Set<Department> departments) { this.departments = departments; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", departments=" + departments + "]"; } }
下面的代码来自Department.java。
package cn..common; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Department implements java.io.Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private long id; private String name; @ManyToMany private Set<Person> persons = new HashSet<Person>(); public Set<Person> getPersons() { return persons; } public void setPersons(Set<Person> persons) { this.persons = persons; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Department [id=" + id + ", name=" + name + ", persons=" + persons + "]"; } }下载 ManyToMany_Join_Table.zip
以下是数据库转储。
Table Name: DEPARTMENT Row: Column Name: ID, Column Type: BIGINT: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Design Row: Column Name: ID, Column Type: BIGINT: Column Value: 2 Column Name: NAME, Column Type: VARCHAR: Column Value: Testing Table Name: DEPARTMENT_PERSON Row: Column Name: DEPARTMENT_ID, Column Type: BIGINT: Column Value: 1 Column Name: PERSONS_ID, Column Type: BIGINT: Column Value: 1 Row: Column Name: DEPARTMENT_ID, Column Type: BIGINT: Column Value: 1 Column Name: PERSONS_ID, Column Type: BIGINT: Column Value: 2 Table Name: PERSON Row: Column Name: ID, Column Type: BIGINT: Column Value: 1 Column Name: NAME, Column Type: VARCHAR: Column Value: Tom Row: Column Name: ID, Column Type: BIGINT: Column Value: 2 Column Name: NAME, Column Type: VARCHAR: Column Value: Jack Table Name: PERSON_DEPT Row: Column Name: PERSON_ID, Column Type: BIGINT: Column Value: 1 Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 1 Row: Column Name: PERSON_ID, Column Type: BIGINT: Column Value: 1 Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 2 Row: Column Name: PERSON_ID, Column Type: BIGINT: Column Value: 2 Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 1 Row: Column Name: PERSON_ID, Column Type: BIGINT: Column Value: 2 Column Name: DEPT_ID, Column Type: BIGINT: Column Value: 2
JPA教程 -JPA查询连接多对一示例以下JPQL显示如何连接多对一映射。SELECT p FROM Professor e JOIN e.phones p)例子下面的代码来...
JPA教程 -JPA查询左连接示例以下代码显示了如何在JPQL中使用左连接。List l = em.createQuery(SELECT e, dFROM Professor e LEFT...
JPA教程 -JPA查询函数示例JPQL支持的函数表达式如下所示。函数描述ABS(number)返回number参数的无符号版本。结果类型与参数类型(...
JPA教程 -JPA查询编号索引参数示例以下代码显示了如何在JPQL中使用数字索引参数。Collection expired = em.createQuery(SELECT u...
Java格式 -Java数字格式类以下两个类可用于格式化和解析数字:java.text.NumberFormatjava.text.DecimalFormat NumberFormat 类可...