Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Hibernate - Force foreign key lookup on insert

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

These are the mappings of my SQL DB:

USERACCOUNT
-------------
Userid (PK) --autoincrement



USERACCOUNTInvoice
-------------
Userid (PK) –-Foreign Key to the above
Invoics(PK) --autoincrement


In my app I create a new UserAccountInvocie object which I persist by doing a session,save(useraccount) I get an error saying

Cannot intert null into UserAccountInvoiceTable for field UserID

NOTE: there are a number of USERACCOUNTInvoice objects in a SET in the USERACCOUNT object. So ideally saving parent will cascade down to child changes

Is there a way of mapping the relationship in the XML so Hibernate knows to use the userID of the parent UserAccount object. I don’t think I should set the userID in USERACCOUNTInvoice manually as I think Hibernate will get very confused if I partially set the composite key. And I don’t want to loop through a the child set and save new USERACCOUNTInvoice either. Unless I have too?





 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
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