Karthik
Karthik

Reputation: 46

How do i use multiple POJOs to update the same table in a database?

I have two objects of Classes A and B (both are mapped to the same table and have a in my hibernate configuration. The tag in both the objects also refer to the primary key of the table in the database.).

When users use my application, at one point in time Object A is updated. Later on Object B is updated. Sometimes, there is an exception that happens and I think its because I'm using mutliple objects like this (message printed below)

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):

Do i need to Override the equals method for these objects (as explained in this question?)

Upvotes: 1

Views: 720

Answers (2)

Yogendra Joshi
Yogendra Joshi

Reputation: 228

May be the old transaction is not committed and flush() is also useful, because there are no guarantees about when the session executes the JDBC calls, only the order in which they are executed - except you use flush().

Upvotes: 0

spiritwalker
spiritwalker

Reputation: 2257

StaleObjectStateException is usually thrown when version check(optimistic lock) failed. It is a quite common thing in concurrent environment. You might need proper logic for handling this exception, such as give the user of failed transaction some useful message like "hi, the data you intend to update was just updated by another user" or "you are trying to update an out of date data" blablabla. All in all, I don't think you need to do anything about mapping.

Upvotes: 2

Related Questions