This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate does not reconcile insert/updates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate does not reconcile insert/updates" Watch "Hibernate does not reconcile insert/updates" New topic
Author

Hibernate does not reconcile insert/updates

Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

I am having a deadlock issue in multi-threading environment if "insert and update of the same object" is performed within the same transaction. Point is that hibernate performs two operations (instead of one) when thread creates one object then updates the same object .
Second operation is deadlocking with another thread that is doing the same job. I expected hibernate reconciles update and insert because both of them are performed before then do a flush. Record that is inserted is not yet persisted when update is requested. It looks like that second update is absolutly undesired.

I tested this issue with HB 2.1 and 3.1 - both with the same results. Configuration is SQL Server, HILO generator is used for PK. Two processes are continiously performing the same operation in the separate JVMs.

As a result I see
insert object x
update object x
Why does not hibernate reconcile insert and update ?

[ October 27, 2005: Message edited by: Stas Sakalou ]
[ October 27, 2005: Message edited by: Stas Sakalou ]

Good luck for yourself.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
The update after an insert could be due a missing

inverse="true"

on a bi-directional association. Take a look at

this post.
Stas Sokolov
Ranch Hand

Joined: Apr 13, 2004
Posts: 117

There are no relationships mapped.
Can you just for curiosity sake, make one simple test. You have to create a class with only one method main. In the main method you have to create a session then initialize a new object, and then save it. Update one of the fields of the object and flush the session. Check hibernate SQL trace. Does it perform 2 operations?
?
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
I tried it. Hibernate performed 2 operations.

Hibernate is mirroring in sql the operations I performed on the POJOs.

That's really the safe approach. It could cause problems if Hibernate combined statements (or eliminated statements in the case of an insert + a delete.)

For example, triggers might not fire or operations might not cascade correctly.

On my current project, I have an inventory value that my application should insert but never update. If Hibernate combined statements, I could end up with an incorrect value.
[ October 30, 2005: Message edited by: Scott Johnson ]
 
 
subject: Hibernate does not reconcile insert/updates