人
类
@Embeddable
public class Person {
@Column
public int code;
//...
}
作为两个不同的属性两次嵌入到 Event
中:manager
和 operator
@Entity
public class Event {
@Embedded
@Column(name = "manager_code")
public Person manager;
@Embedded
@Column(name = "operator_code")
public Person operator;
//...
}
当生成具有持久性的数据库模式时,这应该给出两个相应的列。而是抛出一个异常:
org.hibernate.MappingException: Repeated column in mapping for entity: Event column: code
如何覆盖每个属性的默认列名 code
?
最佳答案
使用@AttributeOverride
, 这是一个例子
@Embeddable public class Address {
protected String street;
protected String city;
protected String state;
@Embedded protected Zipcode zipcode;
}
@Embeddable public class Zipcode {
protected String zip;
protected String plusFour;
}
@Entity public class Customer {
@Id protected Integer id;
protected String name;
@AttributeOverrides({
@AttributeOverride(name="state",
column=@Column(name="ADDR_STATE")),
@AttributeOverride(name="zipcode.zip",
column=@Column(name="ADDR_ZIP"))
})
@Embedded protected Address address;
...
}
在你的情况下它看起来像这样
@Entity
public class Event {
@Embedded
@AttributeOverride(name="code", column=@Column(name="manager_code"))
public Person manager;
@Embedded
@AttributeOverride(name="code", column=@Column(name="operator_code"))
public Person operator;
//...
}
关于java - JPA:如何覆盖@Embedded属性的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36571347/