2016-04-12 44 views
9

PersonJPA:如何重寫@Embedded的列名屬性

@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

+0

使用'@ AssociationOverrides'(用於實體間關係)或'@ AttributeOverrides'(用於簡單的屬性) – Thomas

回答

21

使用@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", 
          [email protected](name="ADDR_STATE")), 
     @AttributeOverride(name="zipcode.zip", 
          [email protected](name="ADDR_ZIP")) 
    }) 
    @Embedded protected Address address; 
    ... 
} 

在你的情況下,它看起來像這樣

@Entity 
public class Event { 
    @Embedded 
    @AttributeOverride(name="code", [email protected](name="manager_code")) 
    public Person manager; 

    @Embedded 
    @AttributeOverride(name="code", [email protected](name="operator_code")) 
    public Person operator; 

    //... 
} 
+0

這對我不起作用,因爲我有兩個相同的'@ Embeddable'類的實例,而不是你的例子中的兩個不同。 –