Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Wed Sep 23, 2009 4:54 pm 
Newbie

Joined: Wed Sep 23, 2009 4:15 pm
Posts: 2
I tried the Hibernate Validator 4.0.0.CR1 with mixed results. In a simple command line proof-of-concept app with one class (a simple Java bean) and no DB, I used the new Validation annotations i.e. @Min, @NotNull, @Size, etc. and everything works well.

However, in a somewhat large Spring 3.0 M4 app using Hibernate core as well as other open-source APIs, I got the following exception in Tomcat when I try to start it (Tomcat) after deploying the WAR while trying to validate a simple Java bean which is not persistent.

The app was built with Maven 2 and I specified the following dependencies:
________________________________________________________

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.0.CR1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
</dependency>


******** Here's the stack trace **********

java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;
org.hibernate.validator.engine.resolver.JPATraversableResolver.isReachable(JPATraversableResolver.java:33)
org.hibernate.validator.engine.resolver.DefaultTraversableResolver.isReachable(DefaultTraversableResolver.java:95)
org.hibernate.validator.engine.resolver.SingleThreadCachedTraversableResolver.isReachable(SingleThreadCachedTraversableResolver.java:47)
org.hibernate.validator.engine.ValidatorImpl.isValidationRequired(ValidatorImpl.java:761)
org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:331)
org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:278)
org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:260)
org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:213)
org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
com.real.mark.auth.domain.CmsContentRepository.validate(CmsContentRepository.java:218)
com.real.mark.auth.domain.CmsContentRepository.createContentId(CmsContentRepository.java:211)
com.real.mark.auth.provider.ContentProviderImpl.getContentItem(ContentProviderImpl.java:68)
com.real.mark.service.ContentAuthorizationServiceImpl.getContentItem(ContentAuthorizationServiceImpl.java:78)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:80)
com.real.mark.service.ExceptionHandlingAspect.doExceptionHandling(ExceptionHandlingAspect.java:48)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:622)
org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:611)
org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:65)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy48.getContentItem(Unknown Source)
com.real.mark.web.ContentController.getContentItem(ContentController.java:25)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:631)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:149)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:363)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:351)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:161)
org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
$Proxy52.handle(Unknown Source)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:766)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:712)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:636)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:545)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:344)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:110)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:79)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:98)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.authentication.AnonymousProcessingFilter.doFilter(AnonymousProcessingFilter.java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.wrapper.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:55)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:36)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.authentication.www.BasicProcessingFilter.doFilter(BasicProcessingFilter.java:177)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:356)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:150)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)

___________________________________________________________

Any help would be much appreciated. Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Thu Sep 24, 2009 6:17 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
However, in a somewhat large Spring 3.0 M4 app using Hibernate core as well as other open-source APIs, I got the following exception in Tomcat when I try to start it (Tomcat) after deploying the WAR while trying to validate a simple Java bean which is not persistent.


What other libraries, especially Hibernate related ones are you using? Maybe you can list all you dependencies? I am guessing that you are having a Hibernate application using JPA 1 via Hibernate EntityManager. What I don't understand is why the JPATraversableResolver gets enabled. This resolver is intended for JPA 2 and will not work in an JPA 1 application. Unless you are explicitly specifying this resolver in the configuration it should not be used.

For that reason it would be great if you could provide some more context on how you are using Validator and how you are bootstrapping it. It might be a problem in the JPA version detection mechanism or a problem with maven (transitive) dependencies.

In case you are explicitly configuring the JPATraversableResolver in a JPA 1 application you should read this thread - viewtopic.php?f=9&t=999747
You will need a custom BeanValidationEventListener in your code and register it with Hibernate in the configuration file.

--Hardy


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Thu Sep 24, 2009 7:15 pm 
Newbie

Joined: Wed Sep 23, 2009 4:15 pm
Posts: 2
Hardy - thank you very much for your prompt response. I appreciate it. As you correctly pointed out this problem only comes up when Hibernate core is involved - and yes - we are using Hibernate 3.3.x I believe on this project along with Spring 3.0M4. A JAR from another team that we're using specifies the dependency on Hibernate in its Maven POM.

I believe that Hibernate 3.3.x has dependencies on both JPA 1 as well as JPA 2 i.e. persistence-api-1.0.jar and ejb3-persistence-1.0.1.GA.jar. Is this correct? I think that the method getPersistenceUtil(...) isn't available in JPA 1 whereas it is available in JPA 2.

I'm not using any listeners or JPA directly - especially not JPATraversableResolver. Somehow this gets invoked in my webapp with the Hibernate core 3.3.x dependencies and not in the simple proof-of-concept app with no Hibernate core dependencies. I'm setting the ValidationFactory from Spring using dependency injection as follows:
<bean id="validationFactory" class="javax.validation.Validation" factory-method="buildDefaultValidatorFactory"/>

This is then injected into a domain class which has a ValidationFactory against which I invoke getValidator(). I then invoke validator.validate(...) as follows:

Set<ConstraintViolation<Car>> constraintViolations = validator.validate(objectToValidate);


Hopefully I've been doing the right things. Is there any other information I can provide?


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Fri Sep 25, 2009 6:43 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Hibernate Core itself does not depend at all on JPA. Hibernate EntityManager does, but all released versions of it depend so far on the JPA 1 specification. JPA 2 work is still in progess and at best experimental. And yes PersistenceUtil is only part of JPA2

What I don't get is how in your case the JPATraversableResolver gets activated (and it does since it gets called) whithout being explicitly configured. In the default case DefaultTraversableResolver should be used which in turn will delegate to JPATraversableResolver, but only if PersistenceUtil is on the classpath. Check the source.

It would be great to get to the bottom of this. Here are a few suggestions:

* Enable debug trace and check the log file (post it here if you like)
* Verify all included jar files. In the maven build you can run eg 'mvn dependency:tree' to get a resolved dependency list. Also check the generated war files and which libraries go into it. Last but not least check the shared lib directories on your tomcat. Which jpa api jars are included in the classpath?
* Start Tomcat with remote debugging enabled, place a breakpoint into DefaultTraversableResolver.detectJPA and find out whether PersistenceUtil can be loaded. If you are not using JPA2, PeristenceUtil should not be in the classpath and the method should return in the first catch block of the method.

--Hardy


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Wed Nov 04, 2009 8:51 pm 
Newbie

Joined: Wed Nov 04, 2009 8:45 pm
Posts: 1
I had the same problem, it was ejb3-persistence-1.0.1.GA.jar, which contained PersistenceUtil. It was fixed in ejb3-persistence-1.0.2.GA.jar, which does not contain this class any more.


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Sun Dec 06, 2009 4:30 am 
Newbie

Joined: Sun Dec 06, 2009 4:23 am
Posts: 2
We have the same problem. We were using the JSR 303 bean validator in an ADF project were there is a TopLink library with a PersistenceUtil.class.
How can we avoid then this error "java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;"?

thanks,
pino


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Mon Dec 07, 2009 8:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Seems TopLink has a incomplete JPA 2 implementation. If javax.persistence.PersistenceUtil is on the classpath there should be in a complete JPA 2 implementation also a javax.persistence.Persistence.getPersistenceUtil() method.

The easiest way to avoid this at the moment would be to bootstrap Bean Validation with a custom TraversableResolver. You can use DefaultTraversableResolver just without the JPA checking code. If you don't care about lazily loaded properties you can just return true in both interface methods. Of course you could also write a TopLink specific TraversableResolver.

--Hardy


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Mon Dec 07, 2009 9:19 am 
Newbie

Joined: Sun Dec 06, 2009 4:23 am
Posts: 2
I believe that there is no issue about Toplinks JPA 2 implementation. As a matter of fact, I can also find getPersistenceUtil() method inside the Persistence class of Toplink library.

I guess the problem is that my project have also have a reference to Java EE 1.5 library with a Persistence class with no getPersistenceUtil() method.

Nevertheless, I will try your suggestion about a custom TraversableResolver.

thanks,

pino


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Tue Dec 22, 2009 2:32 pm 
Newbie

Joined: Wed Nov 18, 2009 5:30 am
Posts: 10
I was getting this error and i tried following:

Configuration<?> configuration = Validation.byDefaultProvider()
.configure();
validatorFactory = configuration.traversableResolver(
new DefaultTraversableResolver()).buildValidatorFactory();

But still getting java.lang.NoSuchMethodError: getPersistenceUtil>

I am using 4.0.2 GA


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Tue Dec 22, 2009 2:37 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
You cannot use the DefaultTraversableResolver which ships with Validator 4.x. It will try to enable (delegate to) the JPATraversableResolver if it finds PersitenceUtil on the classpath. You have to provide your own implementation of this class.

--Hardy


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Wed Dec 23, 2009 2:44 am 
Newbie

Joined: Wed Nov 18, 2009 5:30 am
Posts: 10
Ok, let me implement that and i think its a better approach as it will always turn off JPATraversableResolver even if JPA 2 is being used as application servers come with their own JPA implementations which might be JPA 2 even if i am using JPA 1.

For the time being i have switched back to eclipselink 1.1 as previously i was using eclipselink 1.2 which is JPA 2 preview release. Which is why JPA 2 traversable resolver was getting enabled. And I am not using JPA 2.

But a point that needs clarification, how does hibernate validator comes to know that JPA 2 is being used? And whether that check needs to be verified to see if if getPersistenceUtil method exists or not.

For hibernate's DefaultTraversableResolver, don't you think its constructor should take a boolean that whether to delegate to JPATraversableResolver or not?


Top
 Profile  
 
 Post subject: Re: Exceptions encountered using Hibernate Validator 4.0.0.CR1
PostPosted: Wed Dec 23, 2009 9:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
But a point that needs clarification, how does hibernate validator comes to know that JPA 2 is being used? And whether that check needs to be verified to see if if getPersistenceUtil method exists or not.

Validator cannot know whether JPA 2 is actually used. All it can do is detect whether JPA 2 is available on the classpath. It does that by checking whether PeristenceUtil is on the classpath. The Bean Validation specification actually specifies that a JPA aware traversable resolver has to be used if Persistence is on the classpath (even though the spec does not say how exactly to detect that). Using a JPA aware resolver in case you are not using JPA is not a problem. The problem here was a incomplete implementation.

Quote:
For hibernate's DefaultTraversableResolver, don't you think its constructor should take a boolean that whether to delegate to JPATraversableResolver or not?

We cannot change the default behaviour. However, I could imagine a second constructor used to explicitly disable delegation. This way you would not have to write your own dummy TraversableResolver.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 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.