举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > jpa 一对多映射 JPA 多对多映射示例

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

2023-03-07 01:18 Java教程

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

jpa 一对多映射

JPA 一对多映射是 JPA 中最常用的映射方式,它表示一个实体对象可以关联到多个其他实体对象。在 JPA 中,一对多映射可以通过 @OneToMany 注释来实现,该注释可以应用于实体类的属性上,表示该属性是一个集合,其中包含了多个关联的实体。

要正确配置一对多映射,需要在拥有 @OneToMany 注释的属性上添加 @ManyToOne 注释。@ManyToOne 注释表示该属性是一个引用,指向另一个实体。这样,JPA 将能够正确地将这些实体之间的关联建立起来。

@Entity
public class Employee {

    @Id
    private Long id;

    private String name;

    @OneToMany(mappedBy = "employee")
    private List<Task> tasks;

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

    @Id
    private Long id;

    private String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "employee_id")
    private Employee employee;

   // getters and setters... 
}

JPA 多对多映射示例

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


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

它描述了人与部门之间的关系。一个部门可以有很多人,一个人可以为许多部门工作。

例子

下面的代码来自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
        + "]";
  }

}

下面的代码来自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.ManyToMany;

@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
   
  private String name;
  
  @ManyToMany
  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 + "]";
  }


}

下面的代码来自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;
}
下载 ManyToMany_Map.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_DEPARTMENT
 Row:
    Column Name: PERSON_ID,
    Column Type: BIGINT:
    Column Value: 1

    Column Name: DEPARTMENTS_ID,
    Column Type: BIGINT:
    Column Value: 1

 Row:
    Column Name: PERSON_ID,
    Column Type: BIGINT:
    Column Value: 1

    Column Name: DEPARTMENTS_ID,
    Column Type: BIGINT:
    Column Value: 2

 Row:
    Column Name: PERSON_ID,
    Column Type: BIGINT:
    Column Value: 2

    Column Name: DEPARTMENTS_ID,
    Column Type: BIGINT:
    Column Value: 1

 Row:
    Column Name: PERSON_ID,
    Column Type: BIGINT:
    Column Value: 2

    Column Name: DEPARTMENTS_ID,
    Column Type: BIGINT:
    Column Value: 2


阅读全文
以上是编程学为你收集整理的jpa 一对多映射 JPA 多对多映射示例全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部