如何在休眠中更新组合键

发布于 2024-11-16 14:09:58 字数 1705 浏览 3 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-11-23 14:09:58

您无法更新主键,因为这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文