Using Hibernate Validator with JPA and Spring

孤街醉人 提交于 2019-12-30 11:00:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2410074/using-hibernate-validator-with-jpa-and-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!