• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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

 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic