Issue Details (XML | Word | Printable)

Key: HB-1014
Type: Bug Bug
Status: Closed Closed
Resolution: Rejected
Priority: Major Major
Assignee: Unassigned
Reporter: Thomas Meeks
Votes: 2
Watchers: 3
Operations

If you were logged in you would be able to see more operations.
Hibernate2

Hibernate does not remove ID's on a failed flush

Created: 08/Jun/04 02:15 PM   Updated: 25/Oct/06 10:04 AM
Component/s: core
Affects Version/s: 2.1.4
Fix Version/s: None

Time Tracking:
Not Specified

Environment:
2.1.4
PostgreSQL
Most common in Swing applications


 Description  « Hide
A thread about it: http://forum.hibernate.org/viewtopic.php?t=931005

Conditions to recreate:
1. Create a new hibernate session (and transaction, if desired)
2. Create a transient object (that hibernate can persist)
3. Place the transient object in a known bad state (so that any attempt to persist it will fail)
4. Attempt to save the object, which will fail
5. Close session, rollback txn
6. Fix the error with the object
7. Create a new hibernate session/txn
8. Attempt to save the object (which will currently fail, though it should not)
9. Close session

The error occurs at step 8. Because hibernate does not set the ID of the object to null, it will assume that the object is in the database. When hibernate attempts to update the object (rather than save), it will fail. This bug is particularily nasty when dealing with cascades, as all objects will be assigned numbers.

A workaround:
Have your DAO layer keep track of new objects and manually set the ID to null /after/ the session has closed (if you delete the ID before the session closes, hibernate will complain about the changing of IDs).

This workaround can get quite messy, however, if one requires the use of cascades.

A (possible) real fix:
Have hibernate keep track of all new entities in the session, and upon failure, iterate those entities and set their ID to null.

Another possibility (enumerated by gavin in the thread mentioned above) is to copy the entire object tree by value, but this could cause real problems in objects with large byte arrays.

The problem may be difficult to fix, however, I find the fact that the steps outlined above do not work to be very counterintuitive. If not fixed, this bug should be at the very least well documented.

The exception handling portion of the documentation does not indicate that all newly saved objects must be thrown out if the session they are saved in has an exception.

 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Gavin King added a comment - 08/Jun/04 07:44 PM
This is not a bug. This is as per Hibernate doco. "discard the session and all persistent objects".

Tom Sedge added a comment - 15/Jun/04 10:42 AM
I feel that declaring this not a bug is a cop-out. There's no dispute about the documents - but there is dispute about the design here!

I know that Hibernate cannot possibly unroll all the changes but in many cases it doesn't need to since the application should be aware of these and able to roll them back. The one thing the application can't roll-back is any auto-generated ID assignments.

Therefore it is appropriate that Hibernate remove these on failure. Doing so would make it much easier to cope with situations like this (which are required in my situation too for very good reason).

I strongly urge you to re-consider adding this functionality.

Chris Bohnert added a comment - 03/Mar/05 09:37 AM
I agree with Tom on this issue.

Since Hibernate modifies my objects, adding the identity values during the save(), after a rollback I expect Hibernate to be responsible for rolling back its changes to my in memory objects.


Christian Bauer added a comment - 04/Mar/05 07:05 AM
This has been added to Hibernate3 a while ago, you are welcome to backport it if you need it in Hibernate 2.x.

Arnout Engelen added a comment - 25/Oct/06 10:04 AM
I assume by 'added' you're referring to the hibernate.use_identifer_rollback property (http://www.hibernate.org/hib_docs/v3/reference/en/html/session-configuration.html)?

It didn't appear to work for me, but that might be unrelated.