This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes OneToOne shared PK isn't setting key in related table during insertion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "OneToOne shared PK isn Watch "OneToOne shared PK isn New topic
Author

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

David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Aug 07, 2003
Posts: 1646
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: OneToOne shared PK isn't setting key in related table during insertion
 
Similar Threads
Indexed Collections
Doubt regarding maps key type while bnding
Error when inserting data in hibernate
HibernateTools Generating Wrong Code
How could I map this if possible?