如何使用 JPA + Hibernate 和 Open Session in View 策略在 ConstraintViolationException 之后合并和更新实体?
userTransaction.begin();
try {
em.persist(entity);
} catch (PersistenceException ce){
ConstraintViolationException t = (ConstraintViolationException) ce.getCause();
String constraint = t.getConstraintName();
if (constraint.equals(ConstantesUtil.MY_UK_CONFIG) == false ){
throw new Exception(ce);
} else {
entity = findByUk(entity);
}
} catch (Exception e){
throw new Exception(e);
}
entity.setOtherPropertyValue('forExample');
em.merge(entity);
userTransaction.commit();
我知道发生异常后事务/会话变脏。有什么方法可以清理它并坚持合并吗?