I've been beating my head against a wall with this one and I was hoping someone might be able to help.
I have a number of classes I wish to persist, each of which implements an AddressOwner interface and contains a collection of Address objects, which I'm mapping as one-to-many relationships, using an 'address' table. Because the same class (Address) is used with a number of different 'parent' or 'one' classes, I can't specify the 'one' class in my many-to-one tag for the Address class. I did not think this would matter unduly as I am able to call setAddressOwnerId in the addAdress method all my 'one' classes implement.
This works nicely in the case of previously stored 'one' (AddressOwner) records which have had their ids assigned, but not in the case of new records, where the parent id, which is still null, gets assigned to the child Address.
I understand that if I specify the many-to-one class for the child in the mapping file, then Hibernate first inserts the parent and then the child, using the new parent id. Given that I am not able to specify the many-to-one class, for reasons given above, is there any other way in which I can have Hibernate automatically save the child with the appropriate id when the parent is saved?
|