11

Can you derive from the JPA spec, if @PrimaryKeyJoinColumn(...), which doesn't have the insertable and updatable parameters, is the same as

@JoinColumn(..., insertable = false, updatable = false)

or

@JoinColumn(..., insertable = true, updatable = true)

when used on regular (non-inheritance) associations? Should they be interchangable? What are the insertable and updatable properties set to? Are they set to anything at all? Note, I'm only targeting the read-only attribute that both (seem to) implement...

I'm getting rather inconsistent mapping exception with EclipseLink and Hibernate...

Here's the @PrimaryKeyJoinColumn JavaEE 5 + 6 Javadoc:

PrimaryKeyJoinColumn (JavaEE 5)
PrimaryKeyJoinColumn (JavaEE 6)

Quote:

... and it may be used in a OneToOne mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity.

Kawu
  • 13,647
  • 34
  • 123
  • 195
  • even more - if look at thir recommendation for making one-to-one relation in different versions of API - looks like they more do not recomend use `@PrimaryKeyJoinColumn` for relation creating. [java5](http://docs.oracle.com/javaee/5/api/javax/persistence/OneToOne.html) vs [java6](http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html) – msangel Mar 06 '13 at 15:51

1 Answers1

15

Yes, the two are equivalent.

Note in JPA 2.0 you can also add an @Id to a @OneToOne mapping and avoid having the duplicate basic id attribute altogether.

See

from the WikiBooks Java Persistence pages

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
James
  • 17,965
  • 11
  • 91
  • 146
  • Thanks!: "Because you now have two mappings for the same foreign key column you must define which one will be written to the database (it must be the Basic one), so the OneToOne or ManyToOne foreign key must be defined to be read-only. This is done through setting the JoinColumn attributes insertable and updatable to false, or by using the @PrimaryKeyJoinColumn instead of the @JoinColumn." – Kawu Nov 18 '10 at 16:02
  • This means I will have to file a bug report for EclipseLink, because exchanging @JoinColumn(..., insertable = false, updatable = false) with @PrimaryKeyJoinColumn(...) throws MappingExceptions... – Kawu Nov 18 '10 at 16:04
  • the second link should be: http://en.wikibooks.org/wiki/Java_Persistence/OneToOne#Target_Foreign_Keys.2C_Primary_Key_Join_Columns.2C_Cascade_Primary_Keys – Kawu Nov 18 '10 at 20:23