0

我被它困住了,我试图坚持一个实体(出租车)和一个孩子(库存)。

public void updatePriceByType(Cab cab) { 

  try { 
    for(Inventory inventory : cab.getInventories()){
      inventory.setCab(cab); 
      em.persist(em.merge(inventory));
    } 
    em.persist(em.merge(cab)); 
  } 
  finally {
    if (null != em) { 
      em.close(); 
    } 
  } 
}

获取异常如下图:

Caused by: javax.persistence.RollbackException: java.lang.ClassCastException: org.eclipse.persistence.internal.indirection.QueryBasedValueHolder cannot be cast to org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:512)
... 44 more
Caused by: java.lang.ClassCastException: org.eclipse.persistence.internal.indirection.QueryBasedValueHolder cannot be cast to org.eclipse.persistence.internal.indirection.UnitOfWorkValueHolder
at org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy.buildBackupClone(TransparentIndirectionPolicy.java:92)
at org.eclipse.persistence.internal.indirection.TransparentIndirectionPolicy.backupCloneAttribute(TransparentIndirectionPolicy.java:78)
at org.eclipse.persistence.mappings.ForeignReferenceMapping.buildBackupClone(ForeignReferenceMapping.java:225)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildBackupClone(ObjectBuilder.java:498)
at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.buildBackupClone(DeferredChangeDetectionPolicy.java:219)
at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.revertChanges(DeferredChangeDetectionPolicy.java:283)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.resumeUnitOfWork(UnitOfWorkImpl.java:5248)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.synchronizeAndResume(UnitOfWorkImpl.java:5197)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.synchronizeAndResume(RepeatableWriteUnitOfWork.java:548)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1154)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
4

1 回答 1

0

在 merge() 的结果上调用 persist() 没有意义,因为 merge() 将持久化或更新对象。最初需要调用merge的cab对象是从哪里来的?它是在另一个 EntityManager/事务中读取的还是序列化的?

该错误不应发生,因此您可能希望在其上记录错误。确保您首先尝试最新版本。还要检查您是否没有使用属性访问,并且在您的 get/set 方法中有副作用。

于 2013-01-23T14:35:37.863 回答