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

时间:2016-04-12 11:01:51

标签: java hibernate jpa orm

@If ( @UpperCase ( MyCaptchaField ) = TheRealValueOfMyCaptcha ; FIELD reasontype := "6" ; "") 上课

Person

@Embeddable public class Person { @Column public int code; //... } 内嵌入了两个不同的属性:Eventmanager

operator

在使用Persistence生成数据库模式时,这应该给出两个相应的列。而是抛出异常:

  

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

如何为每个属性覆盖默认列名@Entity public class Event { @Embedded @Column(name = "manager_code") public Person manager; @Embedded @Column(name = "operator_code") public Person operator; //... }

1 个答案:

答案 0 :(得分:37)

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

    //...
}