views:

42

answers:

2

"Table 1" can have multiple instances of "Table 2". "Table 2" is one to one mapped with "Table A". But the instance for "Table A" is created by "Table B". "Table 1" need not always have an entry of "Table 2".

Table 2 has two columns say tb1 and tbA. tb1 references to primary key of "Table 1" and tbA to that of "Table A".

If the data for Table A is saved first and then the data for Table 1 then it works fine. But if I am saving saving the content of Table 1 which has a mapping to Table 2 entry and Table A entry is not saved yet, I am getting a TransientObjectException.

I am using hibernate and annotations in JAVA. I have tried the cascade associations and every other possible solutions that I could find in the web. But of no use. Please do throw in some ideas.

Example : Consider Table 1 = Person; Table 2 = Belonging; Table A = Car; Table B = Manufacturer. Person can have many Car. Car has the person_id (refering the Person) and car_id (the Car). A Car should definitely have a manufacturer. Now in my application, a Person will be associated with a car which is created by the Manufacturer but not saved (i.e. car_id not yet generated). Now when I save the session data as a whole (that is how it can be done in my case) i want the car data to be saved first followed by the Person data bcos then the TransientObjectException will not occur. Any suggestions on how to accomplish this. Hope the example is better.

+2  A: 

See question

Jeroen Rosenberg
While this previous question explains the `TransientObjectException`, I'm not *sure* it will help to solve the OP's problem. But if it does, this question should be closed as dupe. Let's see.
Pascal Thivent
A: 

Your example is incredibly hard to follow and showing some (pseudo) code would probably help. But according to my understanding of your description, you might need session.merge() in your case.

Pascal Thivent