JPA @Embedded和@Emdeddable注解
初用JPA @Embedded和@Emdeddable注解,对其用法不理解,哪位详细解释下注解双方是怎样关联起来的?
以某网志中两个class为例:
File: Address.javaimport javax.persistence.Column;import javax.persistence.Embeddable;@Embeddablepublic 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(); }}File: Professor.javaimport javax.persistence.AttributeOverride;import javax.persistence.AttributeOverrides;import javax.persistence.Column;import javax.persistence.Embedded;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.PrimaryKeyJoinColumn;import javax.persistence.SecondaryTable;import javax.persistence.Table;@Entity @Table(name="EMP") @SecondaryTable(name="EMP_ADDRESS", pkJoinColumns=@PrimaryKeyJoinColumn(name="EMP_ID"))public class Professor { @Id private int id; private String name; @Embedded @AttributeOverrides({ @AttributeOverride(name="street", column=@Column(table="EMP_ADDRESS")), @AttributeOverride(name="city", column=@Column(name="CITY", table="EMP_ADDRESS")), @AttributeOverride(name="state", column=@Column(name="STATE", table="EMP_ADDRESS")), @AttributeOverride(name="zip", column=@Column(name="ZIP_CODE", table="EMP_ADDRESS")) }) private Address address; 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 Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String toString() { return "Professor id: " + getId() + " name: " + getName() + " with " + getAddress(); }}
@AttributeOverride(name="state", column=@Column(name="STATE", table="EMP_ADDRESS")),
@AttributeOverride(name="zip", column=@Column(name="ZIP_CODE", table="EMP_ADDRESS"))
})
表示这个 Address 是个内嵌的对象,并且将其中的属性的映射覆盖在 Address 中默认的定义。可以看一下在 Address 类中有句 @Column(name="ZIP_CODE") 这个,但由于我们在 EMP 类中用的时候,这个字段是属于 EMP_ADDRESS 表的,在 Address 中声明的没有表名,如果这里不覆盖的话就会被认为是 EMP 中的字段。由于这里采用了多表映射,因此 Address 中的列定义需要全部覆盖,重新声明。
另外,需要说一下的是,在 @Embeddable 标注的 POJO 类,拥有默认属性名的 @Column,像 Address 中 private String state; 上面啥都没写,这会默认会是 @Column(name = "state")。