JPA 多對(duì)一兩個(gè)連接列示例

2018-09-27 16:36 更新

JPA教程 - JPA多對(duì)一兩個(gè)連接列示例


以下代碼顯示了如何在多對(duì)一映射中設(shè)置連接列。

@ManyToOne
@JoinColumns({
    @JoinColumn(name="DEPT_NAME", referencedColumnName="Name"),
    @JoinColumn(name="DEPT_ID", referencedColumnName="ID")
})
private Department department;

例子

下面的代碼來自Department.java。

package cn.w3cschool.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 implements java.io.Serializable{
  @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.w3cschool.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.JoinColumns;
import javax.persistence.ManyToOne;

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

  }
  @PersistenceContext
  private EntityManager em;
}
下載 ManyToOne_Two_Join_Columns.zip

以下是數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)。

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_NAME,
    Column Type: VARCHAR:
    Column Value: Design

    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_NAME,
    Column Type: VARCHAR:
    Column Value: Design

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




以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)