JPA:如何覆盖@Embedded属性的列名 [英] JPA: how to override column names of @Embedded attributes
本文介绍了JPA:如何覆盖@Embedded属性的列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Person
类
@Embeddable
public class Person {
@Column
public int code;
//...
}
两次作为两个不同的属性分别嵌入在Event
内:manager
和operator
is embedded inside Event
twice as two different attributes: manager
and 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:实体映射中的重复列:事件列:代码
org.hibernate.MappingException: Repeated column in mapping for entity: Event column: code
如何为每个属性覆盖默认列名称code
?
How to override default column name code
for each attribute?
推荐答案
使用 @AttributeOverride
,这是一个示例
Use @AttributeOverride
, here is an example
@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;
//...
}
这篇关于JPA:如何覆盖@Embedded属性的列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文