-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Thu Feb 18, 2010 11:55 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Hi,

We want to use Bean Validation annotations (package javax.validation.constraints) instead of Hibernate Validator specific ones. Because of incompatibility reasons, we will not be able to migrate to Hibernate 3.5 (JPA 2.0) and thus cannot use the automatic validation between JPA 2.0 and Bean Validation 1.0.

How do we validate entities with Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4 ? At the moment, nothing is triggered when the persist is done (no constraint violation is thrown). We've tried using @EntityListeners(JPAValidateListener.class) from the hibernate-validator-legacy artifact on each entities, but it doesn't work either.

Any idea ?
Thanks,
Antonio


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Thu Feb 18, 2010 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Write your own BeanValidationEventListener and register it as listener in Hibernate. The code this this is pretty much identical to what we do in the latest Hibernate release. It is really independent of JPA2.

Have a look at this thread - viewtopic.php?f=9&t=999747&start=15

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Fri Feb 19, 2010 6:15 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Hi,

Thanks for the information. I had a look at all that, but unfortunately, it doesn't work. I've tried just to validate by hand as follow :

Code:
Country country = new Country(null, null);
Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
Set<ConstraintViolation<Country>> violations = validator.validate(country);
assertTrue(violations.size() == 1);


The problem is that validation uses the JPATraversableResolver class which uses the Persistence.getPersistenceUtil() method. This method is in JPA 2.0, not in JPA 1.0.

I've tried to get a different TraversableResolver (I thought mayby DefaultTraversableResolver could work) by doing :

Code:
TraversableResolver tr = new DefaultTraversableResolver();
validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();


But it still gets the JPATraversableResolver. Any idea ?

Thanks,
Antonio


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Fri Feb 19, 2010 6:23 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Write a TraversableResolver that uses Hibernate.isInitialized instead of the JPA 2 equivalent.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Fri Feb 19, 2010 6:39 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
agoncal wrote:
Code:
TraversableResolver tr = new DefaultTraversableResolver();
validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();


But it still gets the JPATraversableResolver. Any idea ?


DefaultTraversableResolver tries to detect JPA and use the JPAAwareTR if Persistence is found in the classpath. But writing your own TR should work.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.0.2 with Hibernate Entity Manager 3.4
PostPosted: Fri Feb 19, 2010 6:53 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Brilliant, it works. This is what I've done :

Code:
import org.hibernate.Hibernate;

import javax.validation.Path;
import javax.validation.TraversableResolver;
import java.lang.annotation.ElementType;

public class JPATraversableResolver implements TraversableResolver {

    public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
        return traversableObject == null || Hibernate.isInitialized(traversableObject);
    }

    public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
        return true;
    }
}


Then I use my own JPATraversableResolver as follow :

Code:
TraversableResolver tr = new JPATraversableResolver();
Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();

Country country = new Country(null, null);
Set<ConstraintViolation<Country>> violations = validator.validate(country);
assertEquals("Should have 2 constraints violation", 2, violations.size());


Good, now just need to code all that in an entity listener and trigger it at @PrePersist and @PreUpdate.

Thanks Emmanuel
Antonio


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.