JPA 嵌入式示例

2018-02-23 15:48 更新

JPA教程 - JPA嵌入式示例


以下部分演示如何將實(shí)體嵌入到另一個(gè)實(shí)體。

首先,我們通過用@Embeddable注釋標(biāo)記類來創(chuàng)建一個(gè)Embeddable實(shí)體。

@Embeddable @Access(AccessType.FIELD)
public class Address {

然后,當(dāng)將Embeddable實(shí)體添加到其容器類時(shí),使用@Embedded標(biāo)記它。

    
@Entity
public class Employee {
    @Id private int id;
    private String name;
    private long salary;
    
    @Embedded 
    private Address address;

例子

下面的代碼來自PersonDaoImpl.java。

package cn.w3cschool.common;

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

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class PersonDaoImpl {
  public void test() {
    Employee emp = new Employee();
    emp.setName("name");
    emp.setSalary(12345);
    

    Address p = new Address();
    p.setCity("New York");

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

  @PersistenceContext
  private EntityManager em;
}

以下代碼來自Employee.java。

package cn.w3cschool.common;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Employee {
    @Id private int id;
    private String name;
    private long salary;
    
    @Embedded 
    private Address address;
    
    public Employee() {}

    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 Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address; 
    }

    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() +
               " salary: " + getSalary() + " address: " + getAddress();
    }
}

以下代碼來自Address.java。

package cn.w3cschool.common;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable @Access(AccessType.FIELD)
public class Address {
    private String street; 
    private String city; 
    private String state;
    @Column(name="ZIP_CODE")
    private String zip;

    public String getStreet() {
        return street;
    }
    
    public void setStreet(String address) {
        this.street = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }
    public String toString() {
        return "Address street: " + getStreet() +
               ", city: " + getCity() +
               ", state: " + getState() +
               ", zip: " + getZip();
    }

}
下載 Embeddable.zip

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

Table Name: EMPLOYEE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 0

    Column Name: CITY,
    Column Type: VARCHAR:
    Column Value: New York

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

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

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

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

    Column Name: SALARY,
    Column Type: BIGINT:
    Column Value: 12345


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號