2016-04-12 9 views
9

Person वर्गजेपीए: कैसे @Embedded के स्तंभ नाम ओवरराइड करने के लिए जिम्मेदार बताते हैं

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

    //... 
} 

Event दो बार के रूप में दो अलग-अलग विशेषताओं के अंदर एम्बेडेड है: manager और operator

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

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

    //... 
} 

यह दो संबंधित कॉलमों जब देना चाहिए दृढ़ता के साथ डेटाबेस स्कीमा उत्पन्न करना। इसके बजाय एक अपवाद फेंका जाता है:

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

यह मेरे लिए काम नहीं करता है क्योंकि मेरे पास एक ही '@ एम्बेड करने योग्य' वर्ग के दो उदाहरण हैं, आपके उदाहरण में दो अलग नहीं हैं। –

संबंधित मुद्दे