简体   繁体   English

在JPA 2.0中捕获约束违规

[英]Catching constraint violations in JPA 2.0

Consider the following entity class, used with, for example, EclipseLink 2.0.2 - where the link attribute 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 link attribute, 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; 检查DatabaseException上的错误代码 - 我担心这个错误代码是数据库的本机错误代码;
  • Checking the existence of a Profile with the specific value for link beforehand - this obviously would result in an enormous amount of superfluous queries. 事先检查具有link特定值的Profile的存在 - 这显然会导致大量多余的查询。

When I insert records with a duplicate value for the link attribute, EclipseLink does not throw a EntityExistsException 当我为link属性插入具有重复值的记录时,EclipseLink不会抛出EntityExistsException

Yes, and a JPA provider is not supposed to throw an EntityExistException in that case, you won't get an EntityExistException on something else than the primary key. 是的,并且在这种情况下,JPA提供程序不应该抛出EntityExistException ,除了主键之外,您不会获得EntityExistException

(...) but throws a DatabaseException, with the message explaining that the unique constraint was violated. (...)但抛出DatabaseException,并显示消息,说明违反了唯一约束。

This is very WRONG from EclipseLink, a JPA provider should throw a PersistenceException or a subclass but certainly not a specific exception like oepeDatabaseException . EclipseLink非常错误 ,JPA提供程序应抛出PersistenceException或子类,但肯定不是像oepeDatabaseException这样的特定异常。 This is a bug and should be reported as such as I already mentioned in a previous answer . 这是一个错误,应该像我在之前的回答中提到的那样报告

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 . 与上述答案相同,请参阅我之前的答案

It's too bad they don't have a ConstraintViolationException in JPA. 他们在JPA中没有ConstraintViolationException太糟糕了。 I've created a helper method to determine if the PersistenceException is a constraint violation for a given class - it is hibernate only though. 我已经创建了一个辅助方法来确定PersistenceException是否是给定类的约束违例 - 它只是休眠。 I imagine there is a way to do it using other implementations. 我想有一种方法可以使用其他实现来完成它。

protected Boolean isUniqueConstraintViolation(PersistenceException ex, Class entity) {

    if (((NonUniqueObjectException)ex.getCause()).getEntityName().equals(entity.getName())) {
        return true;
    }

    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM