Hi,
I'd like to discuss how to recover state of objects (especially id) when transaction fails.
I'll describe situation: I have simple one to many mappings (person-address) in which I want to add new address. I load person (from database) with e.g. two addresses, I add new address to set of addresses of person and then I'm trying to save person (through cascading it save also newly created address). But because of let's say not null database column (street of address) the transaction fails. Then the state is following: I have person with some addresses and one of the address is not stored in database (but this address has id already assigned because of hibernate).
Then, on client I'm, not able to find out which address is the one which was newly created - because all addresses has id now.
Of course I could keep memory pointer to newly created address but situation can be worse when I e.g. need to send person and addresses through Spring's httpinvoker (and then after transaction fails I need to find which address was newly created) to server or when I need to save larger object graph with many associations. Does some general solution exists? Some description how to work with hibernate in such cases. Or some recommendation?
I'm considering following solutions: Set id back to null for newly created objects through hibernate interceptor. But I hope there is some build in automatic solution.
|