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

2

23

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

I have a simple alternative/natural key (UNIQUE, NOT NULL) column iso_code on my Countries table which I'd like to use in a reference, but Eclipse's Dali shows a validation error and Hibernate throws a MappingException.

Is such a common scenario allowed?

Sihunn answered 28/4, 2011 at 12:10 Comment(0)
S
12

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

Sihunn answered 2/5, 2011 at 9:31 Comment(1)
P
8

Support of relationships that references non-PK columns is an optional feature. In simple cases it's supported by Hibernate, but it can't be used as a part of dervied identity.

However, as long as you don't derive an identity (i.e. if you can set a value of primary key component manually) you can try to play with read-only mappings, something like this:

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

    @Id
    @Column(name = "country_code")
    private String countryCode; // Primary key component should be set manually

    @ManyToOne
    @JoinColumn(name = "country_code", referencedColumnName = "iso_code", 
        insertable = false, updatable = false)
    private Country country = null; // Read-only relationship based on a value 
                                    // of primary key component

    ...
}
Presentday answered 28/4, 2011 at 12:15 Comment(6)
Please look here for the concrete example + code: #5784772 Eclipse Dali shows an error and Hibernate produces a MappingException. I'll have to try EclipseLink now.Sihunn
I have created a new question here: #5819163Sihunn
This is a strange implication of JPA 2.0. JPA 1.0 mappings would allow references to non-PK column. This definitely is a step backward IMHO. Please see my own answer here for more details: #5784772Sihunn
@Kawu: You missed the point. JPA 2.0 allows references to non-PK columns as well, but not in the case of dervied identities. JPA 1.0 doesn't have a concept of derived identities at all. Code snippet above shows how your code might looks in JPA 1.0, without dervied identities. In works in JPA 2.0 as well.Presentday
Ahm no I didn't miss the point. It's exactly what I wrote. There's currently no pure JPA 2.0 way to achieve this. Your mapping truly works, but it's "just" the JPA 1.0 compatible mapping. I was looking for a pure JPA 2.0 way with derived identifiers, which, as you concurr, doesn't exist. But I don't understand why the limitation that references to non-PK columns in JPA 2.0 derived identifiers don't work exists at all!Sihunn
Given that JPA 1.0 compatible mappings can handle this model makes the "new" JPA 2.0 derived identifiers inferior over the JPA 1.0 mappings. Definitely a step into the wrong direction IMO.Sihunn

© 2022 - 2024 — McMap. All rights reserved.