File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate - Force foreign key lookup on insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate - Force foreign key lookup on insert" Watch "Hibernate - Force foreign key lookup on insert" New topic
Author

Hibernate - Force foreign key lookup on insert

Ally Cavs
Ranch Hand

Joined: Aug 25, 2008
Posts: 89
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?





 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate - Force foreign key lookup on insert
 
Similar Threads
1-to-M mapping confusion
Please suggest on this Mapping
Ternary and composite ID
hbm.xml file does an update instead of an insert- Helppppp
One-to-many mapping and composite key