JPA 一对多映射示例

由 haiguiking 创建,youj 最后一次修改 2017-01-09

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



以上内容是否对您有帮助:
java工程师idea
二维码
建议反馈
二维码