File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
David Harkness
Ranch Hand
Posts: 1646
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic