views:

647

answers:

1

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.

+3  A: 

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.

Pascal Thivent