是否可以在 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;
}
通过此设置,我收到错误@Column(s) not allowed on a @OneToOne property。是否可以在 JPA 2.0 中以任何其他方式执行此操作
谢谢 桑杰
最佳答案
这是不可能的。您可以简单地为此类用例添加一个访问器,该访问器委托(delegate)给引用的实体:
public class City {
public String getCountryCode() {
return null == country ? null : country.getCountryCode();
}
}
关于java - JPA 2.0 OneToOne 映射引用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9789701/