I have a structure with several tables in association with each other. It basically looks like this: TableA has an on-to-many relationship to TableB, TableB has an one-to-many relationship to TableC, and so forth for five tables... TableB has a foreign key constraint in regard to TableA, and TableC has a foreign key to TableB.
In my C#-code I start with creating a TableAMapping object, setting the id to 0 (which is the unsaved-value in my Hibernate mapping declaration). Then I go on and create a list of TableBMapping objects, setting their id’s to 0, and the property TableBMapping.TableA property to the newly created TableAMapping object (the foreign key constraint). The same goes for the other object. In the end I assign the TableBMapping list to the TableAMapping.TableB property.
Nothing weird, although it’s hard to explain in text and not in code. The problem arises when I try to save the TableAMapping object to the database. I get a foreign key exception from the database, saying that the TableB.TableA property is faulty. And of cause it is faulty; it is not set since I don’t know the TableA’s id until I save the object to the database.
In my world, shouldn’t Hibernate set TableA’s id automatically? How do I solve this problem? Do I need to save each object on its own?
|