日期:2014-05-20  浏览次数:20723 次

JPA @Embedded和@Emdeddable注解
初用JPA @Embedded和@Emdeddable注解,对其用法不理解,哪位详细解释下注解双方是怎样关联起来的?
以某网志中两个class为例:
Java code

File: Address.java


import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
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();
    }

}


File: Professor.java


import 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();
    }
}



这样Address和Professor在数据库中的表结构是怎样的呢?

------解决方案--------------------
EMP 表:

1. ID
2. NAME

EMP_ADDRESS 表:

1. EMP_ID
2. street
3. CITY
4. STATE
5. ZIP_CODE

这里除了两个不常用的 @Embedded, @Embeddable 外,还有个不常用的 @SecondaryTable

@SecondaryTable 表示一个 Entity 映射了两张表,当然了,用 @SecondaryTables 可以一个 Entity 映射成 N 张表。能这样映射的表需要有个条件,需要有外键引用字段才可以这样做。上面的例子中:

@SecondaryTable(name="EMP_ADDRESS", pkJoinColumns=@PrimaryKeyJoinColumn(name="EMP_ID"))

表示 EMP_ADDRESS 中的 EMP_ID 字段与 @Table(name="EMP") 中标注 @Id 的字段进行关联,由于 @PrimaryKeyJoinColumn 中没有 referencedColumnName 值,因此就默认与 EMP 的主键关联。

@Embeddable 表示这个非 Entity 类可以嵌在一个 Entity 类中作为属性存在。

@Embedded
@AttributeOverrides({
@AttributeOverride(name="street", column=@Column(table="EMP_ADDRESS")),
@AttributeOverride(name="city", column=@Column(name="CITY", table="EMP_ADDRESS")),