保存具有复杂关系的实体时的StaleStateException

编程入门 行业动态 更新时间:2024-10-09 20:30:18
本文介绍了保存具有复杂关系的实体时的StaleStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在数据库(Oracle)中保存的hibernate实体具有非常复杂的关系,因为它有许多相关实体。它看起来像这样......

The hibernate entity I am saving in the database (Oracle) has very complex relations, in the sense that it has many related entities. It looks something like this...

@Table(name = "t_HOP_CommonContract") public class Contract { @Id private ContractPK id; @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @PrimaryKeyJoinColumn private ContractGroupMember contractGroupMember; @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @JoinColumns({ @JoinColumn(name = "TransactionId", referencedColumnName = "TransactionId"), @JoinColumn(name = "PrimaryContractId", referencedColumnName = "PrimaryContractId") }) @Fetch(FetchMode.SUBSELECT) private List<ContractLink> contractLinks; // . . . . . . . // A couple of more one to many relationships // Entity getters etc. }

我还有几个实体,比如......

I also have a couple of more entities such as...

@Table(name = "t_HOP_TRS") public class TotalReturnSwap { @Id private ContractPK id; // Entity Getters etc. }

诀窍是我有在同一笔交易中持续执行合约和 TotalReturnSwap 实体。

The trick is that I have to do persistence of Contract and TotalReturnSwap entities in the same transaction.

有时它可能是一堆必须在同一事务中持久存在的实体。

Sometimes it could be a bunch of entities that have to be persisted in the same transaction.

当我保存 TotalReturnSwap 实体(在我保存合约实体后总是这样做。)

I have noticed the following exception when I save the TotalReturnSwap entity (which is always done after I have saved the Contract entity).

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:675) \ at org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793) at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664) at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) at org.springframework.transaction.support.AbstractPlatformTransactionManagermit(AbstractPlatformTransactionManager.java:723) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:147) at com.rbs.fcg.publishing.DownstreamContractBusinessEventPostingService.performTDWPersistenceForContracts(DownstreamContractBusinessEventPostingService.java:102) at com.rbs.fcg.publishing.DownstreamContractBusinessEventPostingService.persistContractBusinessEvent(DownstreamContractBusinessEventPostingService.java:87) at com.rbs.fcg.publishing.DownstreamContractBusinessEventPostingService.publish(DownstreamContractBusinessEventPostingService.java:67) at com.rbs.fcg.publishing.PublishingProcessor.publish(PublishingProcessor.java:76) at com.rbs.fcg.publishing.PublishingProcessor.process(PublishingProcessor.java:52) at com.rbs.are.MultiThreadedQueueItemProcessor$2.run(MultiThreadedQueueItemProcessor.java:106) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:662) Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85) at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)

现在有几点可以帮助回答问题:

Now a few points that may help while answering questions:

  • 我只在数据库中保存(插入)实体 - 从不更新/删除/阅读
  • 我已经能够隔离这个异常,即使在单个即使我们的应用程序是多线程的,但是线程环境也不像多线程问题
推荐答案

错误可能由以下几点引起:

The error can be caused by several things:

  • 在提交对象之前刷新数据可能会导致清除所有对象等待持久化。
  • 如果在提交之前清理对象,则对象具有自动生成的主键并且您正在强制分配的键
  • 数据库的对象。
  • 零或不正确的ID:如果您将ID设置为零或其他,Hibernate将尝试更新而不是插入。
  • 对象是陈旧的:Hibernate从会话中缓存对象。如果对象被修改,并且Hibernate不知道它,它将抛出此异常 - 请注意StaleStateException
  • Flushing the data before committing the object may lead to clear all object pending for persist.
  • If object has primary key which is auto generated and you are forcing an assigned key
  • if you are cleaning the object before committing the object to database.
  • Zero or Incorrect ID: If you set the ID to zero or something else, Hibernate will try to update instead of insert.
  • Object is Stale: Hibernate caches objects from the session. If the object was modified, and Hibernate doesn’t know about it, it will throw this exception — note the StaleStateException
  • 我是没有得到它的信任,发现它这里。

    I'm not taking the credit for it, found it here.

    更多推荐

    保存具有复杂关系的实体时的StaleStateException

    本文发布于:2023-10-23 02:48:37,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1519522.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实体   关系   StaleStateException

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!