如何在休眠中更新组合键
我有一个包含 8 列的表格 C1,C2,C3,C4,C5,C6,C7,C8,其中组合 {C1,C2,C3} 是一个复合键。
我想仅更新 C2 的数据,但是当我尝试更新时,它显示异常:
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)
是否可以在 Hibernate 中更新此数据?
我正在使用代码--
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法更新主键,因为这是 Hibernate 在您更新后用来检索对象的方法。这就是为什么你会得到陈旧异常的原因,因为 Hibernate 期望一个具有相同主键的对象。
因此,基本上,您必须将主键更改为其他值,例如递增数字或克隆对象,保存它,然后删除旧的不正确的对象。
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.