【问题标题】:how to update composite keys in hibernate如何在休眠中更新复合键
【发布时间】:2011-06-23 11:04:38
【问题描述】:

我有一个包含 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();

}

【问题讨论】:

  • 无法更新关系数据库中的主键列。也许您可以使用代理键?

标签: java hibernate composite-key


【解决方案1】:

您无法更新主键,因为这是 Hibernate 在更新后用于检索对象的内容。这就是为什么你得到陈旧异常的原因,因为 Hibernate 期望一个具有相同主键的对象。

因此,基本上您必须将主键更改为其他值,例如递增数字或克隆对象保存它,然后删除旧的不正确对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多