JPA: 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.

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

Support of relationships that references non-PK columns is an optional feature. In simple cases it's supported by 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, updateable = false) private Country country = null; // Read-only relationship based on a value // of primary key component ... }.

Please look here for the concrete example + code: stackoverflow. Com/questions/5784272/… Eclipse Dali shows an error and bernate produces a MappingException. I'll have to try EclipseLink now.

– Kawu Apr 28 at 12:19 I have created a new question here: stackoverflow. Com/questions/5818663/… – Kawu Apr 28 at 12:35 @Kawu: Updated. – axtavt Apr 28 at 12:52 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: stackoverflow.Com/questions/5784272/… – Kawu Apr 29 at 8:13 @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. – axtavt Apr 29 at 8:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions