JPA 和 Bean 验证的唯一约束
我希望 Bean Validation 有一个 @Unique
约束,但这不是标准提供的。如果我使用 JPA 的 @UniqueConstraint
我就不会有独特的验证和错误报告机制。
有没有办法将@Unique定义为Bean验证约束并将其与JPA结合起来,以便JPA创建具有唯一约束的列并检查值是否唯一?
I'd like to have a @Unique
constraint with Bean Validation, but that is not provided by the standard. If I would use JPA's @UniqueConstraint
I wouldn't have a unique validation and error reporting mechanism.
Is there a way to define @Unique
as a Bean Validation constraint and combine it with JPA, such that JPA creates a column with an unique constraint and checks wheter a value is unique or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非获得整个表的锁,否则基本上不可能使用 SQL 查询检查唯一性(任何并发事务都可以在手动检查之后但在正在进行的事务提交之前修改数据) 。换句话说,不可能在 Java 级别实现有效的唯一验证,从而提供验证实现。检查唯一性的唯一可靠方法是在提交事务时。
BV 规范总结如下:
因此,虽然我同意将唯一(且非空)约束违规包含在 Bean 验证异常中会很好,但目前情况并非如此。
参考
Unless you acquire a lock on a whole table, it is basically not possible to check for unicity using a SQL query (any concurrent transaction could modify data after a manual check but before the commit of the ongoing transaction). In other words, it isn't possible to implement a valid unique verification at the Java level and thus to provide a validation implementation. The only reliable way to check for unicity is while committing the transaction.
The BV spec summarizes it like this:
So while I agree that it would be nice to have unique (and non null) constraint violations wrapped in a Bean Validation exception, this is currently not the case.
References
有关如何实现 @Unique 及其周围问题的更多信息可以在这里找到 - http://community。 jboss.org/wiki/AccessingtheHibernateSessionwithinaConstraintValidator
More information on how to implement a @Unique and the problematic around it can be found here - http://community.jboss.org/wiki/AccessingtheHibernateSessionwithinaConstraintValidator
好吧,你可以做到,但这并不是一件小事。问题是:验证器需要访问数据库来执行一些查询来检查您要插入的值是否已经存在。这实际上不能从验证器完成,因为它无权访问 sessionFactory/会话。当然,您可以在验证器内实例化它(session/sessionFactory),但这不是一个好的编码实践。
Well you CAN do it, but it's not trivial. The problem is: the validator requires database access to perform some queries to check, if the value you want to insert is already there or not. And this can't be really done from the validator, as it doesn't have access to the sessionFactory/session. Of course you could instantiate it (session/sessionFactory) inside the validator, but it's not a good coding practice.
您可以让验证器读取 JPA 注释并应用它。这是一个使用 spring 验证器的示例,可以用作扩展的想法。
JPA JSR303 Spring表单验证
你也可以注入(
@Inject
或 Spring 的@Autowired
)自定义验证器中的会话 bean 和容器应该知道如何连接它。我只知道这是一个Spring的例子:You can make a validator read the JPA annotations and apply it. Here is somewhat of an example using spring validators that can be used as an idea to expand on.
JPA JSR303 Spring Form Validation
You can also inject (
@Inject
or Spring's@Autowired
) your session bean in a custom validator and the container should know how to wire it up. I only know this as a Spring example:您应该尝试(插入或更新),捕获异常并执行一些操作。例如,在 JSF 支持 bean 中:
另一个选项是在插入/修改之前进行检查。这可能非常耗时,并且不能防止错误最终发生。
You should try (insert or update), catch the exception and do some action. For example in a JSF backing bean :
Another option is to check before the insertion/modification. This can be time consuming and doesn't prevent the error to happen in the end.