Does the JPA specification allow references to non-primary key columns?

@axtavt: It appears as if your answer wasn’t correct. I’ve just received an email from the authors of “Pro JPA 2.0”, who were also working on the JPA spec themselves.

“In your example the Zip class has a relationship to a Country:

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

This appears to be trying to point the country_code foreign key column to the iso_code column in the Country table, which is not a PK. JPA has never allowed you to create a relationship like this because without specifying the PK of the Country
there would be no way to uniquely identify which Country instance is in the relationship. You are simply hitting the problem when you get to the derived identifier part, but the problem looks to be in the invalid relationship itself.”

So the JPA spec doesn’t allow relationships/FKs to non-PK columns at all…

Leave a Comment