2016-04-12 16 views
9

Person lớpJPA: làm thế nào để ghi đè lên tên cột của @Embedded thuộc tính

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

    //... 
} 

được nhúng vào bên trong Event gấp đôi so với hai thuộc tính khác nhau: manageroperator

@Entity 
public class Event { 
    @Embedded 
    @Column(name = "manager_code") 
    public Person manager; 

    @Embedded 
    @Column(name = "operator_code") 
    public Person operator; 

    //... 
} 

Điều này sẽ cho hai cột tương ứng khi tạo lược đồ cơ sở dữ liệu với Persistence. Thay vào đó là một ngoại lệ được ném:

org.hibernate.MappingException: Lặp đi lặp lại cột trong bản đồ cho tổ chức: Cột tổ chức sự kiện: Mã

Làm thế nào để ghi đè lên tên cột mặc định code cho mỗi thuộc tính?

+0

này sử dụng '@ AssociationOverrides' (đối với các mối quan hệ tổ chức) hoặc' @ AttributeOverrides' (đối với các thuộc tính đơn giản) – Thomas

Trả lời

21

Sử dụng @AttributeOverride, đây là một ví dụ

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

Trong trường hợp của bạn nó sẽ trông như

@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

Điều này không hiệu quả đối với tôi vì tôi có hai trường hợp của cùng lớp '@ Embeddable', không phải hai trường hợp khác nhau như trong ví dụ của bạn. –

Các vấn đề liên quan