java 在 JPA 2.0 中捕获约束违规
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2892801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catching constraint violations in JPA 2.0
提问by wen
Consider the following entity class, used with, for example, EclipseLink 2.0.2 - where the linkattribute is not the primary key, but unique nontheless.
考虑以下实体类,例如,与 EclipseLink 2.0.2 一起使用 - 其中link属性不是主键,但仍然是唯一的。
@Entity
public class Profile {
@Id
private Long id;
@Column(unique = true)
private String link;
// Some more attributes and getter and setter methods
}
When I insert records with a duplicate value for the linkattribute, EclipseLink does not throw a EntityExistsException, but throws a DatabaseException, with the message explaining that the unique constraint was violated.
当我为link属性插入具有重复值的记录时,EclipseLink 不会抛出EntityExistsException,而是抛出DatabaseException,并带有解释违反唯一约束的消息。
This doesn't seem very usefull, as there would not be a simple, database independent, way to catch this exception. What would be the advised way to deal with this?
这似乎不是很有用,因为不会有一种简单的、独立于数据库的方法来捕获此异常。处理这个问题的建议方法是什么?
A few things that I have considered are:
我考虑过的一些事情是:
- Checking the error code on the
DatabaseException- I fear that this error code, though, is the native error code for the database; - Checking the existence of a
Profilewith the specific value forlinkbeforehand - this obviously would result in an enormous amount of superfluous queries.
- 检查错误代码
DatabaseException- 我担心这个错误代码是数据库的本机错误代码; - 预先检查
Profile具有特定值的 a 是否存在link- 这显然会导致大量多余的查询。
采纳答案by Pascal Thivent
When I insert records with a duplicate value for the link attribute, EclipseLink does not throw a EntityExistsException
当我为链接属性插入具有重复值的记录时,EclipseLink 不会抛出 EntityExistsException
Yes, and a JPA provider is not supposed to throw an EntityExistExceptionin that case, you won't get an EntityExistExceptionon something else than the primary key.
是的,EntityExistException在这种情况下,JPA 提供程序不应该抛出 an ,EntityExistException除了主键之外,您不会得到其他东西。
(...) but throws a DatabaseException, with the message explaining that the unique constraint was violated.
(...) 但会抛出一个 DatabaseException,并带有解释违反唯一约束的消息。
This is very WRONGfrom EclipseLink, a JPA provider should throw a PersistenceExceptionor a subclass but certainly not a specific exception like o.e.p.e.DatabaseException. This is a bug and should be reported as such as I already mentioned in a previous answer.
这在 EclipseLink 中是非常错误的,JPA 提供程序应该抛出一个PersistenceException或一个子类,但肯定不是像o.e.p.e.DatabaseException. 这是一个错误,应该像我在之前的回答中提到的那样报告。
This doesn't seem very usefull, as there would not be a simple, database independent, way to catch this exception. What would be the advised way to deal with this?
这似乎不是很有用,因为不会有一种简单的、独立于数据库的方法来捕获此异常。处理这个问题的建议方法是什么?
Same answer as above, see my previous answer.
与上面的答案相同,请参阅我之前的答案。
回答by Brian DiCasa
It's too bad they don't have a ConstraintViolationException in JPA. I've created a helper method to determine if the PersistenceException is a constraint violation for a given class - it is hibernate only though. I imagine there is a way to do it using other implementations.
太糟糕了,他们在 JPA 中没有 ConstraintViolationException。我创建了一个辅助方法来确定 PersistenceException 是否违反给定类的约束——尽管它只是休眠状态。我想有一种方法可以使用其他实现来做到这一点。
protected Boolean isUniqueConstraintViolation(PersistenceException ex, Class entity) {
if (((NonUniqueObjectException)ex.getCause()).getEntityName().equals(entity.getName())) {
return true;
}
return false;
}

