2011-12-09 203 views
0
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 

我在插入一個實體到db與persist()方法時得到了這個rollbackexception .....這個異常背後的原因是什麼?事務回滾?

回答

2

檢查您是否已正確設置所有雙向關係(在您的com.model.Product實體和與其相關的其他實體之間)。

例如,如果有一個從Product一個多1關係Manufacturer你會碰到這樣的:

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

將檢查你hbm文件之前,可能會出現一些問題級聯(set cascade=all)。