java JPA:如何覆盖@Embedded 属性的列名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36571347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA: how to override column names of @Embedded attributes
提问by Oleg Mikhailov
Person
class
Person
班级
@Embeddable
public class Person {
@Column
public int code;
//...
}
is embedded inside Event
twice as two different attributes: manager
and operator
Event
作为两个不同的属性被嵌入两次:manager
和operator
@Entity
public class Event {
@Embedded
@Column(name = "manager_code")
public Person manager;
@Embedded
@Column(name = "operator_code")
public Person operator;
//...
}
This should give two respective columns when generating database schema with Persistence. Instead an exception is thrown:
使用持久性生成数据库模式时,这应该给出两个相应的列。而是抛出异常:
org.hibernate.MappingException: Repeated column in mapping for entity: Event column: code
org.hibernate.MappingException:实体映射中的重复列:事件列:代码
How to override default column name code
for each attribute?
如何覆盖code
每个属性的默认列名?
回答by Predrag Maric
Use @AttributeOverride
, here is an example
使用@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;
...
}
In your case it would look like this
在你的情况下,它看起来像这样
@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;
//...
}