Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Updating composite primary column

Unnati Redkar
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Looking at updating an hibernate composite key object for modified either or all of the composite key fields. I tried :

1. load the object.
2. create new object for new composite key values.
3. set the not null fields for new object from the old t this new object
including the associations, while making the primary id of each associate
object as null - since the id generator: class=increment for all of these
4. hibernate.update(new object)
5. delete the old object : hibernate.delete(old obect)

This gives me an error message : failed to lasily initialise the association.
If i comment the piece of code for setting null for primary keys of the all the associations for the new object - constarint violation exception is thrown.

What am i missing? will my update for composite key, update the corresponding associations as well for their new updated foreign key ?

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, isn't updating a primary key somewhat against the whole idea of a primary key?

Shouldn't it just be a simple load of the record (object), evict the object, delete a clone of the object, set the primary keys of the evicted record to zero, (which will save the record with new a new primary/compound key), and then save the entity, which will force the creation of a new primary key/compound key set.

-Cameron McKenzie
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!