違反唯一性約束時,將引發javax.persistence.RollbackException
。但可能有多種理由拋出RollbackException
。我怎麼能發現一個獨特的約束被違反?如何處理JPA唯一約束違規?
try {
repository.save(article);
}
catch(javax.persistence.RollbackException e) {
// how to find out the reason for the rollback exception?
}
...然後解析'getCause()。getMessag()'? – deamon 2010-08-17 12:28:47
這取決於你會得到什麼異常。如果它是'SQLException',則檢查錯誤和SQLStatus。但是一些像Spring這樣的框架將它們轉化爲更有意義的東西('DataIntegrityViolationException')。 – 2010-08-18 07:38:09