问题描述
我使用Hibernate Validator 4.0.2,Spring 3.0和Hibernate 3.3.2(据我所知,它是JPA2之前的版本)作为JPA 1供应商。
我发现很容易将Validator集成到MVC层(它只是起作用),但无法看到如何将验证器自动集成到JPA entityManager(JPA 1)中。
基本上,我有一些实体将被保留,但不是来自web图层,因此尚未经过验证。我希望能够通过验证器预执行器来运行它们。
有没有一种简单的方法可以做到这一点?我知道,如果我使用的是JPA 2提供程序(如Hibernate 3.5发布时),它几乎是自动的。这就是我正在寻找的。 p>
I'm using Hibernate Validator 4.0.2, Spring 3.0 and Hibernate 3.3.2 (which, as I understand it, is pre-JPA2) as a JPA 1 provider.
I've found it easy to integrate the Validator into the MVC layer (it just works) but can't see how to integrate the validator automatically into the JPA entityManager (JPA 1).
Basically, I have some entities that will be persisted but that do not come from the web layer and have therefore not already been validated. I'd like a neat way of running them through the validator pre-persist.
Is there an easy way of doing this? I'm aware that if I was using a JPA 2 provider (like Hibernate 3.5 when it is released), it'd be almost automatic. That's roughly what I'm looking for.
You need to write an entity listener and to trigger validation on @PrePersist, @PreUpdate and even @PreRemove (there are valid use-case for that). See Bean Validation with JPA 1.0 for a code sample.
这篇关于在JPA和Spring中使用Hibernate验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!