我正在使用Play Framework,并且具有以下代码:
import org.hibernate.exception.ConstraintViolationException;
...
public class AuthorService extends Controller
...
public static void delete(Long id)
{
Author author = Author.findById(id);
if(author == null) {
renderError("Attempt to delete author has failed! The entry could not be found.", 500);
}
try {
author.delete();
} catch (ConstraintViolationException e) {
renderError("A foreign key violation exception was thrown trying to delete <b>" + author.name + "</b>!", 500);
}
renderGSON(jobDescription);
}
...
}
我没有使用级联来删除记录,因为必须将外来项目设置为稍后由用户选择的条目。
我的问题是,从未捕获到ContraintViolationException,但我的IDE报告它被抛出?为什么是这样?我想抓住它,处理它并正常继续应用程序。
答案 0 :(得分:2)
因为在Play中,根据code(删除调用_delete),该方法只会抛出javax.persistence.PersistenceException和RuntimeException。
答案 1 :(得分:0)
我不熟悉游戏,但是当您的会话被刷新时(在交易结束时,通常是提交),可能会发生约束违规异常。