交易回滚?

时间:2011-12-09 04:58:53

标签: java database jpa ejb

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

在使用persist()方法将实体插入数据库时​​,我得到了这个rollbackexception .......这个异常背后的原因是什么?

2 个答案:

答案 0 :(得分:2)

检查您是否已正确设置所有双向关系(在com.model.Product实体与其相关的其他实体之间)。

例如,如果从ProductManufacturer之间存在多个关系,您可能会遇到以下情况:

Product product = new Product();
product.setId(1);

Manufacturer manufacturer = new Manufacturer ();
manufacturer.setId(123);
product.setManufacturer(manufacturer); // set Many-1 side of relationship

Set<Product> products = new HashSet<Product>();
products.add(product);
manufacturer.setProducts(products); // set 1-Many side of relationship

答案 1 :(得分:1)

在插入之前检查hbm个文件,可能会出现一些级联问题(set cascade=all)。