JPA 嵌入式示例
2018-02-23 15:48 更新
JPA教程 - JPA嵌入式示例
以下部分演示如何将实体嵌入到另一个实体。
首先,我们通过用@Embeddable注释标记类来创建一个Embeddable实体。
@Embeddable @Access(AccessType.FIELD) public class Address {
然后,当将Embeddable实体添加到其容器类时,使用@Embedded标记它。
@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
以下是数据库转储。
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
以上内容是否对您有帮助:
更多建议: