第二百五十五节 JPA教程 - JPA 多对多连接表示例

JPA教程 - JPA 多对多连接表示例

以下代码显示如何为多对多映射设置连接表。

例子

下面的代码来自PersonDaoImpl.java。

复制代码
package cn.w3cschool.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.w3cschool.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.w3cschool.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
        + "]";
  }

}

以下是数据库转储。

复制代码
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
相关推荐
齐 飞8 分钟前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb
云空9 分钟前
《Python 与 SQLite:强大的数据库组合》
数据库·python·sqlite
暮毅13 分钟前
10.Node.js连接MongoDb
数据库·mongodb·node.js
wowocpp16 分钟前
ubuntu 22.04 server 格式化 磁盘 为 ext4 并 自动挂载 LTS
服务器·数据库·ubuntu
九圣残炎17 分钟前
【从零开始的LeetCode-算法】1456. 定长子串中元音的最大数目
java·算法·leetcode
wclass-zhengge19 分钟前
Netty篇(入门编程)
java·linux·服务器
童先生21 分钟前
Go 项目中实现类似 Java Shiro 的权限控制中间件?
开发语言·go
lulu_gh_yu22 分钟前
数据结构之排序补充
c语言·开发语言·数据结构·c++·学习·算法·排序算法
成富39 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
songqq2740 分钟前
SQL题:使用hive查询各类型专利top 10申请人,以及对应的专利申请数
数据库·sql