Reputation: 1649
I have a table with 8 columns C1,C2,C3,C4,C5,C6,C7,C8, where combination of {C1,C2,C3} is a composite key.
I want to update only C2's data, but when I try to update that it's showing an exception:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1007)
Is it possible to update this in Hibernate?
I am using code --
public static synchronized Session getSession() {
Session mysession = (Session) DAO.session.get();
if (mysession == null||!mysession.isOpen()) {
mysession = sessionFactory.openSession();
DAO.session.set(mysession);
}
return mysession;
}
protected void begin() {
try{
getSession().beginTransaction();
}catch(Exception ex)
{
LOGGER.error(ex);
}
} public void update(Facebook facebookdata){ begin();
getSession().update(facebookdata);
commit();
}
Upvotes: 2
Views: 8420
Reputation: 21
You can't update the primary key because that is what Hibernate is using to retrieve the object after you update it. That is why you get the stale exception since Hibernate was expecting an object with the same primary key.
So basically you would have to change your primary key to something else like an incrementing number or clone the object save it and then delete the old incorrect object.
Upvotes: 2