一尘不染

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

hibernate

Person

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

    //...
}

在使用Persistence生成数据库架构时,应该分别提供两列。而是抛出一个异常:

org.hibernate.MappingException:实体映射中的重复列:事件列:代码

如何覆盖code每个属性的默认列名?


阅读 286

收藏
2020-06-20

共1个答案

一尘不染

使用@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;

    //...
}
2020-06-20