The set returned from oObject1.getObject2() (which you throw away) is the persistent set associated with oObject1. The HashSet that you later create is transient, not associated with the DB at all. So when you save oObject1, hibernate is trying to update an in-memory persistent set from an in-memory transient set. Knowing that the user is always right, it assumes that it's in-memory persistent set reflects an old view of the database, and has to abort the save until you tell it what the actual DB state is.
Either use merge()/replicate() to save this, or don't call oObject1.setObject2(). Instead, you should use oObject1.getObject2().add(oData) to add an item to the set, or just edit oData without touching oObject1.getObject2() at all.
The general rule is: if an object is returned by hibernate, use that instance of the object for edits that you want to see down in the database. Creating new instances and getting them to overwrite in-memory instances is a tricky business, best avoided if possible.
|