Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

@PrimaryKeyJoinColumn: no insertable and no updatable

 
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

why doesn't @PrimaryKeyJoinColumn/s have insertable = x and updatable = x ?

This would imply that if there are any properties to be flagged as "not insertable" and/or "not updatable" should go to the (column) properties marked with @Column rather than the conflicting associations tagged with @JoinColumn/s, right? The question is basically: where do the insertable and/or updatable best go to, the associations or the respective column properties?

Karsten
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically because a PrimaryKeyJoinColumn is always read-only (insertable/updateable=false). It only purpose is to be used when the primary key is mapped using a basic attribute. The basic attribute is responsible for mapping it.

See,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_Relationships

In JPA 2.0 you don't need the basic, so can just use the JoinColumn with an @Id on it.
If you want both, and want the OneToOne writable, then use a JoinColumn and make the Id's column insertable/updateable=false.
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate Python MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(I've already read the wikibooks article a thousand times.)

OK, this is what I suspected. Your statement "@PrimaryKeyJoinColumn is always read-only" makes perfect sense to me. However, I have the following mapping which gives me headaches with your answer. (I'm in a JPA 1.0 environment (JavaEE 5) with Hibernate 3.5.4)

Table first:



As you can see there's a composite PK on the table with game_id being an FK column to Games.id. is_home is a simple PK column which goes into the PK class. That's about it. Here's the (generated) JPA 1.0 code:



Composite PK class using @EmbeddedId:



Hibernate 3.5.4 produces a stack trace on calling Persistence.createEntityManagerFactory("bbstats"):



Hibernate is complaining about game_id not being insertable=false and updatable=false. According to your statement and my suspicion the @PrimaryKeyJoinColumn should do just that. Note, that I put the insertable = false, updatable = false on the ID class' game_id property, because when using an @IdClass implementation instead of @EmbeddedId "insertable = false, updatable = false" is put into the redundant column's @Id annotation, too. (it might be wrong to do so)

I was able to get rid of the Hibernate exception by using

@ManyToOne
@PrimaryKeyJoinColumn(name = "game_id", referencedColumnName = "id", insertable = false, updatable = false)
private Game game = null;

to give:



This however means one of two things:

1. Your statement and my suspicion are wrong.
or
2. Hibernate has an issue correctly interpreting @PrimaryKeyJoinColumn

It raises the question for me, as I'm trying to create JPA 1.0 code to be used with Hibernate, if Hibernate 3.x can be used in a JPA 1.0 environment at all.

See: https://coderanch.com/t/509568/ORM/java/Hibernate-JPA

For me, it's a mystery why

@PrimaryKeyJoinColumn(name = "game_id", referencedColumnName = "id")
and
@JoinColumn(name = "game_id", referencedColumnName = "id", insertable = false, updatable = false)

aren't the same in Hibernate 3.5 and/or 3.6 (assuming JPA 1.0, which doesn't allow adding @Id to @XToX associations)...

Comments?

Karsten
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic