I have a parent class (classA) which has a set of associated objects (classB) in it. classA maps to table A, and classB maps to table B. Table B has Table A's foreign key. In the mapping file, I set the <generator> for the id in both classA and classB as 'identity' (for Microsoft Sql Server).
I've instantiated several classB objects in memory, add them to a set, and add the set to classA object. Now I want to save them to the Database. Here comes the dilemma, how will Hibernate know that it needs to save classA to table A first, so a primary key can be created (the key should be created by database because I set the generator type as 'identity'), and later can be inserted into table B as the foreign key when classB objects are saved? I did set the cascading property. It still didn't work.
Thanks.
|