举报投诉联系我们 手机版 热门标签 鳄鱼CMS
您的位置:鳄鱼CMS > jpa对象嵌套 JPA 嵌入式共享示例

jpa对象嵌套 JPA 嵌入式共享示例

2023-03-23 19:31 Java教程

jpa对象嵌套 JPA 嵌入式共享示例

jpa对象嵌套

JPA对象嵌套是指在JPA实体中嵌套另一个JPA实体,以便在一个实体中存储另一个实体的信息。这种嵌套关系可以用来表示多对一(Many-to-One)或者一对一(One-to-One)的关系。

JPA对象嵌套有助于减少数据库表的数量,并使得数据库表之间的关系更加清晰。此外,使用JPA对象嵌套还可以减少代码量,因为不需要再创建新的实体来表示多对一或者一对一的关系。

@Entity 
public class Person { 

    @Id 
    private Long id; 

    private String name; 

    @OneToOne(cascade = CascadeType.ALL) 
    private Address address; 

    // getters and setters... 
}
@Entity 
public class Address { 

    @Id 
    private Long id; 

    private String street; 

    private String city;  

    // getters and setters...  
}

JPA 嵌入式共享示例

JPA教程 - JPA嵌入式共享示例


对于嵌入式实体,我们可以在不同实体之间共享它们。

当嵌入它时,我们也可以覆盖可嵌入的实体。

以下代码首先创建一个Address实体。

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

然后创建一个Company类并引用Embeddable实体。

@Entity
public class Company {
    @Id private int id;
    
    @Embedded
    private Address address;
    

之后,它再次在Employee类中嵌入Address类。这一次它覆盖了来自Address类的属性。

@Entity
public class Employee {
  @Id private int id;
  private String name;
  private long salary;
  
  @Embedded 
  @AttributeOverrides({
      @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")),
      @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE"))
  })
  private Address address;

例子

以下代码来自Address.java。

package cn..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();
    }

}

以下代码来自Employee.java。

package cn..common;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
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 
  @AttributeOverrides({
      @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")),
      @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE"))
  })
  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();
  }
}

以下代码来自Company.java。

package cn..common;


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


@Entity
public class Company {
    @Id private int id;
    
    @Embedded
    private Address address;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Address getAddress() {
        return this.address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

    public String toString() {
        return "Company id: " + getId() + " address: " + getAddress();

    }
    
}

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

    Employee emp = new Employee();
    emp.setName("name");
    emp.setSalary(12345);
    

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

    emp.setAddress(p);
    
    Company c = new Company();
    c.setAddress(p);
    c.setId(1);
    
    em.persist(emp);
    
    em.persist(c);
  }

  @PersistenceContext
  private EntityManager em;
}
下载 Embeddable_Share.zip

以下是数据库转储。

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

    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





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: PROVINCE,
    Column Type: VARCHAR:
    Column Value: null

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

    Column Name: POSTAL_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


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