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... }
对于嵌入式实体,我们可以在不同实体之间共享它们。
当嵌入它时,我们也可以覆盖可嵌入的实体。
以下代码首先创建一个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
JPA教程 -JPA 一对一映射示例本节介绍如何在JPA映射中执行一对一映射。此示例假设一个人只能是一个部门的一部分,而一个部门只能...
JPA教程 -JPA双向多对多映射示例以下代码显示如何创建双向多对多映射。例子以下代码来自Professor.java。package cn..common;imp...
JPA教程 -JPA多对一连接列示例以下代码标记了在多对一映射中使用的连接列。@ManyToOne (cascade=CascadeType.ALL)@JoinColumn(na...
JPA教程 -JPA查询检索多对一映射示例以下查询从多对一映射检索实体。List l = em.createQuery(SELECT d.employees FROM Departme...