保存具有复杂关系的实体时出现 StaleStateException
我保存在数据库 (Oracle) 中的休眠实体具有非常复杂的关系,因为它具有许多相关实体.它看起来像这样......
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.
}
诀窍是我必须在同一事务中对 Contract
和 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
实体时注意到以下异常(这总是在我保存 Contract
实体后完成).
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.AbstractPlatformTransactionManager.commit(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 将尝试更新而不是插入.
- Object is Stale:Hibernate 缓存会话中的对象.如果对象被修改了,而 Hibernate 不知道它,它会抛出这个异常——注意 StaleStateException
我没有把它归功于它,发现它 这里.
I'm not taking the credit for it, found it here.
相关文章