一意の制約例外をキャッチする必要がある DAO があります。これを行うには、唯一の有効な解決策は、永続化後に EntityManager をフラッシュすることです。そうして初めて、例外を除外する必要がある catch ブロックに入ります。また、DAO メソッドをトランザクション (REQUIRES_NEW) でラップする必要があります。そうしないと、RollBackException が発生します。
私は何か間違ったことをしていますか?
try {
em.persist(myObject);
em.flush();
} catch (PersistenceException ex) {
if (ex.getCause() != null) {
String cause = ex.getCause().toString();
if (cause != null) {
if (cause.contains("org.hibernate.exception.ConstraintViolationException")) {
logger
.error("org.hibernate.exception.ConstraintViolationException: possible unique constraint failure on name");
throw ex;
}
}
}
}