我需要在实体监听器中持久保存新的实体对象, 但它只在第一次通话时工作正常。
请参阅以下代码:
public class SomeListener{
@PreUpdate
public void preUpdate(SomeEntity o){
EntityManager em= EntityManagerUtils.getEntityManager();
em.persist(new OtherObj());
}
}
public class EntityManagerUtils {
protected static EntityManager getEntityManager() throws Exception {
Object sb = new InitialContext().lookup(JNDI_EntityLisenterSB);
Method method = sb.getClass().getMethod("getEntityManager");
return (EntityManager) method.invoke(sb);
}}
在第二次通话后低于错误日志:
ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffffac19b619:7c4a5bbb:56cfb972:19e, org.eclipse.persistence.transaction.JTASynchronizationListener@7bece143 >: java.lang.NullPointerException
...
javax.ejb.EJBTransactionRolledbackException: Transaction rolled back
...
Caused by: javax.transaction.RollbackException: ARJUNA016053: Could not commit transaction.
但新对象已保存到数据库
at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1177)
at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126)
at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:92) [jboss-as-ejb3-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
... 94 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.updateObjectForWriteWithChangeSet(DatabaseQueryMechanism.java:1005)
任何人都可以提供帮助吗?非常感谢。
工作环境
答案 0 :(得分:0)
提交实体后,该实体将分离。通过第一次调用persist()
执行提交,导致您的实体分离。您可以做的是在再次呼叫另一个persist()
之前再次开始交易。
getTransaction().begin();