Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

OneToOne shared PK isn't setting key in related table during insertion

 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two tables that use the same primary key generated for the "master" table in a one-to-one relationship, and I'm using JPA annotations with Hibernate 3.5.6.
When I create a new master and slave and save them together by saving the master, a 0 value is inserted into slave.master_id instead of the generated value from master.id. The problem is that it's not copying the generated ID from the Master instance to its Slave instance. If I change Slave's masterId type to Integer I get an exception instead:
I'm using Spring and Hibernate which I last used a few years ago so I'm basically starting over. I copied this from the example:
and the code that saves the objects is quite basic:
Specifying a cascadeType (ALL) has no effect. Am I missing a step? While my model does allow a Master to be saved without a Slave, I'd rather not save the Master, attach the Slave, copy the generated ID over, and save again. It just doesn't feel right.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no luck, but after a lot more research I've switched to a bi-directional relationship.
Master hasn't changed, and I get a new exception:
However, in my DAO I dump out the references between the master and slave; both are valid, non-null objects. That means that either the exception is completely erroneous or Hibernate/Spring is setting the slave's master reference to null after persisting the master but before persisting the slave. That makes no sense.
 
Screaming fools! It's nothing more than a 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
    Bookmark Topic Watch Topic
  • New Topic