eclipsemappingjpa-2.0composite-primary-keyjboss-tools

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


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

JPA 2.0 mappings (It might probably suffice to consider only the Zip and ZipId classes as this is where the error seems to come from):

@Entity
@Table(name = "GeoAreas")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class GeoArea implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected Integer id;

    @Column(name = "name")
    protected String name;

    ...
}

@Entity
@Table(name = "Countries")
@DiscriminatorValue(value = "country")
public class Country extends GeoArea
{
    @Column(name = "iso_code")
    private String isoCode;

    @Column(name = "iso_nbr")
    private String isoNbr;

    @Column(name = "dial_code")
    private Integer dialCode = null;

    ...
}

@Entity
@Table(name = "Zips")
@IdClass(value = ZipId.class)
public class Zip implements Serializable
{
    @Id
    @Column(name = "code")
    private String code;

    @Id
    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code")
    private Country country = null;

    ...
}

public class ZipId implements Serializable
{
    private String country;

    private String code;

    ...
}

Pretty simple design:

A country is a geo area and inherits the ID from the root class. A ZIP code is unique within its country so it combines an ISO code plus the actual ZIP code as PK. Thus Zips references Countries.iso_code, which has an alternative unique, not-null key on it (reference to non-primary key column!). The Zip.country association gets an @Id annotation and its variable name is the same as the one in its ID class ZipId.

However I get this error message from within Eclipse (also using JBoss Tools):

Validation Message: "The attribute matching the ID class attribute country does not have the correct type java.lang.String"

  1. Why is this wrong in JPA 2.0 syntax (see @Id annotation on Zip.country)? I don't think it is. After all the types of Zip.country and ZipId.country can't be the same for JPA 2 because of the @Id annotation on the @ManyToOne and the PK being a simple integer, which becomes the ID class counterpart. Can anyone check/confirm this please?
  2. Could this be a bug, probably in JBoss Tools? (Which software component is reporting the above bug? When putting the 3 tables and entity classes into a new JavaSE project there's no error shown with the exact code...)

Solution

  • 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.