gpt4 book ai didi

java - JPA:如何覆盖@Embedded属性的列名

转载 作者:太空狗 更新时间:2023-10-29 22:42:56 24 4
gpt4 key购买 nike

@Embeddable
public class Person {
@Column
public int code;

//...
}

作为两个不同的属性两次嵌入到 Event 中:manageroperator

@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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com