如果Embeddable对象已定义与实体的OneToOne
关系。使用Embeddable对象的实体如何覆盖关系的列名称。
使用以下实体:
@Entity
public class User {
@Id
private int id;
@Embedded
private CubeLocation cubeLocation;
}
@Embeddable
public class CubeLocation {
@OneToOne
private MailBox mailBox;
// .. other non-relevant fields
}
@Entity
public class MailBox {
@Id
private String name;
private String cell;
}
用户实体的数据库表将具有ID和MAILBOX_NAME列。如何将数据库列的名称从MAILBOX_NAME更改为MBX_ID?
我试图为
AssociationOverride
实体定义一个User
批注,但是在这种情况下,我得到此错误: @Entity
@AssociationOverride(
name="cubeLocation.mailBox",
joinColumns=@JoinColumn(name="MBX_ID"))
public class User {
}
错误:
org.hibernate.AnnotationException:非法尝试定义一个
@JoinColumn和一个appedBy关联:cubeLocation.mailBox
最佳答案
关联重写应放在可嵌入对象上:
@Entity
public class User {
@Id
private int id;
@Embedded
@AssociationOverride(
name="mailBox",
joinColumns=@JoinColumn(name="MBX_ID"))
private CubeLocation cubeLocation;
}