JPA 一对多映射示例

JPA教程 – JPA一对多映射示例

以下代码显示了如何在JPA中执行一对多映射。

部门可以有一个或多个员工。在Java代码中,我们可以创建一个Java Bean来表示Employee的Employee类,以及一个Department类来表示一个部门。

当链接员工和部门时,我们引用Employee的Department类如下。

@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;

  private String name;

  @ManyToOne
  private Department department;

我们使用 @ManyToOne 来标记部门以指示关系部门和员工之间。

例子

下面的代码来自Department.java。

package cn.w3cschool.common;

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

@Entity
public class Department {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;

  private String name;

  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;
  }

}

下面的代码来自Person.java。

package cn.w3cschool.common;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;

  private String name;

  @ManyToOne
  private Department department;

  public Person() {}

  public Person(String name) {
    this.name = name;
  }

  public Department getDepartment() {
    return department;
  }

  public void setDepartment(Department department) {
    this.department = department;
  }

  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 + "]";
  }

}

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.common;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test(){
    Department d = new Department();
    d.setName("Design");

    Person p1 = new Person("Tom");
    p1.setDepartment(d);

    Person p2 = new Person("Jack");
    p2.setDepartment(d);

    em.persist(p1);
    em.persist(p2);
    em.persist(d);

  }
  @PersistenceContext
  private EntityManager em;
}

下载 OneToMany_Map.zip

以下是数据库转储。

Table Name: DEPARTMENT
 Row:
    Column Name: ID,
    Column Type: BIGINT:
    Column Value: 1

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

Table Name: PERSON
 Row:
    Column Name: ID,
    Column Type: BIGINT:
    Column Value: 1

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

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

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

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

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

版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《JPA 一对多映射示例》
文章链接:https://zhuji.vsping.com/294666.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。