Saving Objects in Hibernate can be Challenging
From Zanecorpwiki
This is an area where one can clearly see why I believe is no better than direct DB/SQL data management. It's easy for saves and updates to fail, and the reason is often not at all clear.
On the plus side, the failure conditions may indicate areas where mistakes are made, and thus the failure by Hibernate is saving you from the even worse problem of silent data corruption. The problem is that the proper way to save data is much harder to understand and implement.
In other words, Hibernate is better at protecting you from data corruption problems, but it causes a lot more problems in the first place.
Cascading
At the conceptual level, there's this idea of "cascading" changes. So if I have objects A and B where B is in A's object graph[notes 1] then if I save/update/delete A, that those actions will (selectively) cascade through to B. At first blush, this should be one of those things that makes Hibernate better. In theory, you can safely save multiple objects in one go.
In practice, I've just never seen this work. Granted, I haven't really looked into it (my experience being on an inherited system), but there's plenty of places all over our code where cascading changes is clearly the right thing to do and the configuration appears that cascading is set up, yet nothing cascades. This should be something you get without even trying. Instead, you get something that is both complex and opaque.
Unsaved Graphs
The most common problem I run into an error about "failed to save/update due to modified/non-committed row" which then references some object referenced by the object you're trying to save.[notes 2] This relates to the cascading issue in that you need to, but have failed, to properly cascade your changes.
This can be because you need to manually save the referenced object:
hibernateSession.saveOrUpdate(a.getB()); // without this line, saving/committing changes to a would fail hibernateSession.saveOrUpdate(a);
Another common cause is declaring:
<version name="version" column="version" unsaved-value="null"/>
but using a primitive (int or long) as the version instead of the object versions (Integer or Long). When saving a new object, Hibernate sees '0' instead of 'null' and so things the object is not new.


