2012-03-20 86 views
3

是否有可能引用單個屬性中引用的實體,在OneToOne關係JPA 2.0 OneToOne映射參考的屬性

實施例:

@Entity 
    public class Country { 
    @Id 
    private Long countryId; 
    @Column(name="code") 
    private String countryCode; 
    ... 
    } 

@Entity 
public class City { 

    @Id 
    private Long cityId; 

    @OneToOne 
    @JoinColumn(name="countryId",referencedColumnName="cityId") 
    @Column(name="code") 
    private String countryCode; 

}

採用這種設置,我得到@OneToOne屬性不允許出現@Column錯誤。是否有可能在JPA 2.0

THX做到這一點任何其他方式 桑傑

回答

0

這是不可能的。您可以簡單地添加一個訪問器,用於代表參考實體的這種用例:

public class City { 
    public String getCountryCode() { 
    return null == country ? null : country.getCountryCode(); 
    } 
}