We ran into a TransientObjectException in our Hibernate-enabled code. At first the exception was very hard to reproduce and only occured in very rare cases. Sometimes during our integration tests and sometimes during our smoke tests. We tried just about everything to find out why Hibernate interpreted the relevant objects as being unsaved (ie transient) even though we verified that they were indeed saved in a previous Hibernate Session by looking at the sql logging. It turns out that Hibernate does not see the diffference between the values "0" and "null" when looking at the ID property (a java.lang.Long) to check whether the object is persistent. Because our Oracle sequence starts at 0, the first object that was persisted had an ID of 0. When that object was then used in a later Session, Hibernate did not recognize that value as valid and interpreted the object as transient. We fixed this by adding an extra unsaved-value attribute to the relevant mapping. Like so: [code]
MY_SEQ
[/code] That solved the problem. Whether this is a bug or a feature in Hibernate I'll leave as an exercise for the readers :)
Written by

Age Mooij
Our Ideas
Explore More Blogs

The Unwritten Playbook: Leading Without a Title
The Unwritten Playbook: Leading Without a Title Leading Without a Title: The Consultant’s Guide to Stealth Influence “Why should we listen...
Jethro Sloan

The Unwritten Playbook: Winning as a Team
The Unwritten Playbook: Winning as a Team Team Whispering: Navigating the Human Dynamics No One Prepared You For "We’ve gone through three...
Jethro Sloan

