Why is this JPA 2.0 mapping giving me an error in Eclipse/JBoss Tools?

后端 未结 3 665
盖世英雄少女心
盖世英雄少女心 2021-01-23 04:02

I have the following situation:
(source: kawoolutions.com)

JPA 2.0 mappings (It might probably suffice to consider only the Zip and ZipId class

相关标签:
3条回答
  • 2021-01-23 04:04

    I'd say focus your attention on the CompoundIdentity relationship. See this question, and my answer there Help Mapping a Composite Foreign Key in JPA 2.0

    ZipId has no "country" field in your case

    0 讨论(0)
  • 2021-01-23 04:08

    I have not tested your code, but it looks pretty much related to the use of the @PrimareKeyJoinColumn annotation.

    The JPA 2.0 specification in section 11.1.40 states:

    The PrimaryKeyJoinColumn annotation is used to join the primary table of an entity subclass in the JOINED mapping strategy to the primary table of its superclass; it is used within a SecondaryTable annotation to join a secondary table to a primary table; and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity[108].

    The example in the spec looks like your case.

    @Entity
    @Table(name="CUST")
    @Inheritance(strategy=JOINED)
    @DiscriminatorValue("CUST")
    public class Customer { ... }
    
    @Entity
    @Table(name="VCUST")
    @DiscriminatorValue("VCUST")
    @PrimaryKeyJoinColumn(name="CUST_ID")
    public class ValuedCustomer extends Customer { ... }
    

    I hope that helps!

    0 讨论(0)
  • 2021-01-23 04:29

    Answering own question...

    The way I modeled the reference, I use a String because the FK points to the iso_code column in the Countries table which is a CHAR(2), so basically my mapping is right. However, the problem is that JPA 2.0 doesn't allow anything but references to primary key columns. This is what the Eclipse Dali JPA validator shows.

    Taken from "Pro JPA 2.0" by Keith/Schincariol p.283 top, "Basic Rules for Derived Identifiers" (rule #6): "If an id attribute in an entity is a relationship, then the type of the matching attribute in the id class is of the same type as the primary key type of the target entity in the relationship (whether the primary key type is a simple type, an id class, or an embedded id class)."

    Personal addendum:

    I disagree with JPA 2.0 having this limitation. JPA 1.0 mappings allow references to non-PK columns. Note, that using JPA 1.0 mappings instead isn't what I'm looking for. I'd rather be interested in the reason why this restriction was imposed on JPA 2.0. The JPA 2.0 is definitely limiting.

    0 讨论(0)
提交回复
热议问题