gpt4 book ai didi

java - 交易中止

转载 作者:行者123 更新时间:2023-12-02 08:04:06 24 4
gpt4 key购买 nike

我的代码是这样的..

@PersistenceContext(unitName = "myPU")
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void registerManufactuerer(Manufacturer manufacturer) throws Exception {
if (manufacturer.getId() != null) {
for (Product product : manufacturer.getProducts()) {
em.persist(product);
em.remove(product);
}
em.persist(manufacturer);
throw new Exception("Successfully registered");
} else {
throw new Exception("Registration is not possible");
}
}

当我运行该项目时,我遇到了一个异常......那就是......

Caused by: javax.transaction.RollbackException: Transaction marked for rollback.
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:473)
at com.sun.enterprise.transaction.JavaEETransactionManagerSimplified.commit(JavaEETransactionManagerSimplified.java:852)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5114)
... 63 more
Caused by: java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.model.Product[ id=1 ].
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:302)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:695)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1482)
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.issueSQLbeforeCompletion(UnitOfWorkImpl.java:3135)
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.issueSQLbeforeCompletion(RepeatableWriteUnitOfWork.java:344)
at org.eclipse.persistence.transaction.AbstractSynchronizationListener.beforeCompletion(AbstractSynchronizationListener.java:157)
at org.eclipse.persistence.transaction.JTASynchronizationListener.beforeCompletion(JTASynchronizationListener.java:68)
at com.sun.enterprise.transaction.JavaEETransactionImpl.commit(JavaEETransactionImpl.java:435)
... 65 more

其实...什么是理性?

最佳答案

错误消息告诉您的原因是:

During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.model.Product[ id=1 ]

您的产品引用了另一个尚未持久化的实体,并且该关系未定义持久化级联。因此,在数据库中找不到该新实体的 ID,因此您的产品还无法持久保存。

很可能是制造商,所以先坚持制造商。

顺便说一句,为什么你先保留一个产品,然后立即再次删除它?

关于java - 交易中止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8444251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com