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

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

2023-03-04 02:27 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 单向一对多映射示例


以下代码显示了如何进行单向一对多映射。

例子

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

    Phone p = new Phone();
    p.setNumber("1234509876");;

    emp.addPhone(p);
    em.persist(p);
    em.persist(emp);
  }

  @PersistenceContext
  private EntityManager em;
}

以下代码来自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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

@Entity
public class Professor {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private long salary;
    
    @OneToMany
    @JoinTable(name="EMP_PHONE", 
          joinColumns=@JoinColumn(name="EMP_ID"),
          inverseJoinColumns=@JoinColumn(name="PHONE_ID"))
    private Collection<Phone> phones;

    public Professor() {
        phones = new ArrayList<Phone>();
    }

    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 addPhone(Phone phone) {
        if (!getPhones().contains(phone)) {
            getPhones().add(phone);
        }
    }
    
    public Collection<Phone> getPhones() {
        return phones;
    }
    
    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() + 
               " with " + getPhones().size() + " phones";
    }
}

以下代码来自Phone.java。

package cn..common;

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

@Entity
public class Phone {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    @Column(name="NUM")
    private String number;
    private String type;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getNumber() {
        return number;
    }
    
    public void setNumber(String phoneNo) {
        this.number = phoneNo;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String phoneType) {
        this.type = phoneType;
    }

    public String toString() {
        return "Phone id: " + getId() + 
               ", no: " + getNumber() +
               ", type: " + getType();
    }
}
下载 OneToMany_Unidirectional.zip

以下是数据库转储。

Table Name: EMP_PHONE
 Row:
    Column Name: EMP_ID,
    Column Type: INTEGER:
    Column Value: 1

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





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

    Column Name: NUM,
    Column Type: VARCHAR:
    Column Value: 1234509876

    Column Name: TYPE,
    Column Type: VARCHAR:
    Column Value: null





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


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