[英]JPA: how to override column names of @Embedded attributes
Person
@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;
//...
}
在使用Persistence生成數據庫模式時,這應該給出兩個相應的列。 而是拋出異常:
org.hibernate.MappingException:實體映射中的重復列:事件列:代碼
如何覆蓋每個屬性的默認列名稱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;
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.