Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...