举报投诉联系我们 手机版 热门标签 鳄鱼CMS
您的位置:鳄鱼CMS > jpa 一对多映射 JPA 双向多对多映射示例

jpa 一对多映射 JPA 双向多对多映射示例

2023-03-09 16:31 Java教程

jpa 一对多映射 JPA 双向多对多映射示例

jpa 一对多映射

JPA 一对多映射是 JPA 中最常用的映射方式,它表示一个实体对象可以关联到多个其他实体对象。在 JPA 中,一对多映射可以通过 @OneToMany 注释来实现。

@OneToMany 注释由三个属性组成,分别是 targetEntity、mappedBy 和 cascade。targetEntity 属性指定了要关联的目标实体;mappedBy 属性指定了在目标实体中的哪个字段表示这种关联;cascade 属性指定了当更新一方时,另一方也会被更新的情况。

@Entity
public class Department {

    @Id
    private Long id;

    private String name;

    @OneToMany(targetEntity = Employee.class, mappedBy = "department", cascade = CascadeType.ALL)
    private List<Employee> employees;

    // getters and setters...
}
@Entity
public class Employee {

    @Id
    private Long id;

    private String name;

    @ManyToOne(targetEntity = Department.class)
    private Department department;

    // getters and setters...
}

JPA 双向多对多映射示例

JPA教程 - JPA双向多对多映射示例


以下代码显示如何创建双向多对多映射。

例子

以下代码来自Professor.java。

package cn..common;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Professor {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  private long salary;
  
  @ManyToMany 
  private Collection<Project> projects;

  public Professor() {
      projects = new ArrayList<Project>();
  }

  public int getId() {
      return id;
  }
  
  public void setId(int id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }

  public long getSalary() {
      return salary;
  }

  public void setSalary(long salary) {
      this.salary = salary;
  }
  
  public void addProject(Project project) {
      if (!getProjects().contains(project)) {
          getProjects().add(project);
      }
      if (!project.getEmployees().contains(this)) {
          project.getEmployees().add(this);
      }
  }

  public Collection<Project> getProjects() {
      return projects;
  }

  public String toString() {
      return "Employee id: " + getId() + " name: " + getName() + 
             " with " + getProjects().size() + " projects";
  }
}

下面的代码来自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() {

    Professor emp = new Professor();
    emp.setName("name");
    emp.setSalary(12345);
    

    Project p = new Project();
    p.setName("Chem");

    p.getEmployees().add(emp);
    emp.getProjects().add(p);
    em.persist(p);
    em.persist(emp);
  }

  @PersistenceContext
  private EntityManager em;
}

以下代码来自Project.java。

package cn..common;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

@Entity
public class Project {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    protected int id;
    protected String name;
    @ManyToMany(mappedBy="projects")
    private Collection<Professor> employees;

    public Project() {
        employees = new ArrayList<Professor>();
    }

    public int getId() {
        return id;
    }
    
    public void setId(int projectNo) {
        this.id = projectNo;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String projectName) {
        this.name = projectName;
    }
    
    public Collection<Professor> getEmployees() {
        return employees;
    }
    
    public void addEmployee(Professor employee) {
        if (!getEmployees().contains(employee)) {
            getEmployees().add(employee);
        }
        if (!employee.getProjects().contains(this)) {
            employee.getProjects().add(this);
        }
    }
    
    public String toString() {
        return "Project id: " + getId() + ", name: " + getName() +
               " with " + getEmployees().size() + " employees";
    }
}
下载 ManyToMany_Bidirectional.zip

以下是数据库转储。

Table Name: PROFESSOR
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: name

    Column Name: SALARY,
    Column Type: BIGINT:
    Column Value: 12345





Table Name: PROFESSOR_PROJECT
 Row:
    Column Name: EMPLOYEES_ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: PROJECTS_ID,
    Column Type: INTEGER:
    Column Value: 1





Table Name: PROJECT
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NAME,
    Column Type: VARCHAR:
    Column Value: Chem


阅读全文
以上是鳄鱼CMS为你收集整理的jpa 一对多映射 JPA 双向多对多映射示例全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 鳄鱼CMS eyucms.com 版权所有 联系我们