我使用spring jpa和JPARepository编写实体CURD,我想在提交之前比较实体值;该怎么做?
我尝试通过@PreUpdate中的id通过db获取实体,但是抛出异常,如果我添加isSynchronizationActive,则事务已提交。
if (action.startsWith("Pre")) {
scriptService.runScriptByCode(code, mutableMapOf<String, Any>("entity" to ent));
} else if (TransactionSynchronizationManager.isSynchronizationActive()) {
println("CommonListener code is ${code}")
scriptService.runScriptByCode(code, mutableMapOf<String, Any>("entity" to ent));
}
code code is com.khidi.message.domain.StatisMsg::PreUpdate
code code is com.khidi.message.domain.StatisMsg::PreUpdate
2019-06-12 11:23:45.756 ERROR 20244 --- [nio-9093-exec-1] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [java.lang.StackOverflowError]
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:541)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:746)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:714)