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

jpa 多对一 JPA 多对一连接列示例

2023-03-15 10:31 Java教程

jpa 多对一 JPA 多对一连接列示例

jpa 多对一

JPA 多对一(Many-to-One)是 JPA 中最常用的关联关系之一,它表示一个实体可以关联到多个实体。在 JPA 中,多对一的关联关系由 @ManyToOne 注释来表示。

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "department_id")
    private Department department;

    // getters and setters, constructors, toString()...
}

上面的代码段中,Employee 实体有一个多对一的关联到 Department 实体。@ManyToOne 注释表明 Employee 和 Department 之间存在多对一的关联关系。@JoinColumn 指定了外键列名为 department_id。

JPA 多对一连接列示例

JPA教程 - JPA多对一连接列示例


以下代码标记了在多对一映射中使用的连接列。

@ManyToOne (cascade=CascadeType.ALL)
@JoinColumn(name="DEPT_ID")
private Department department;

例子

下面的代码来自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.OneToMany;
import javax.persistence.OrderBy;

@Entity
public class Department {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
  
  private String name;
  
  @OneToMany(targetEntity=Person.class, mappedBy="department")
  @OrderBy("name ASC")
  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 javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Person {
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  private long id;
   
  private String name;
  
  @ManyToOne (cascade=CascadeType.ALL)
  @JoinColumn(name="DEPT_ID")
  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..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);
    
    d.getPersons().add(p1);
    d.getPersons().add(p2);
    
    em.persist(p1);
    em.persist(p2);
    em.persist(d);

  }
  @PersistenceContext
  private EntityManager em;
}
下载 ManyToOne_Join_Column.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: DEPT_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: DEPT_ID,
    Column Type: BIGINT:
    Column Value: 1


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