Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 11:11 am 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
I am using hibernate validator for first time I tested in jetty it worked fine , when I deployed in weblogic I get this class cast exception

Code:
java.lang.ClassCastException: kodo.persistence.PersistenceProviderImpl cannot be cast to javax.persistence.spi.PersistenceProvider


in weblogic-application.xml I added

Code:
      <package-name>javax.validation.*</package-name>
      <package-name>org.hibernate.validator.*</package-name>         


in preferrred-application-packages, I tought this will take care but not , please tell me what should be the entires in preferrred-application-packages, for hibernate validaotr to work in weblogic 10.3


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 12:15 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
what is this "kodo.persistence.PersistenceProviderImpl" ? afaik kodo is now openjpa, I would expect it to use org.apache.openjpa.persistence.PersistenceProviderImpl instead, as mentioned on http://openjpa.apache.org/integration.html

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 1:03 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
s.grinovero wrote:
what is this "kodo.persistence.PersistenceProviderImpl" ? afaik kodo is now openjpa, I would expect it to use org.apache.openjpa.persistence.PersistenceProviderImpl instead, as mentioned on http://openjpa.apache.org/integration.html

I am using validator statically , I call it in my code , Its not by default handled by hibernate , My JPA provider is hibernate , what should I add in preferrred-application-packages to use apache openjap ?


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 1:32 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
s.grinovero wrote:
what is this "kodo.persistence.PersistenceProviderImpl" ? afaik kodo is now openjpa, I would expect it to use org.apache.openjpa.persistence.PersistenceProviderImpl instead, as mentioned on http://openjpa.apache.org/integration.html



My application is spring ,hibernate . I use JPA annotations . Please advice me who is PersistenceProvider in this case ?
I had no class loading issues till I added the hibernate validator , I am assuming If I add the persistance provider which comes with hiberate in weblogic-application.xml issue might be resolved , I tried to find the implementation class for PersistenceProvider but none was found, does hibernate comes with a persistance provider ?
if not Do I have to use any other api like apache openJPA ? please advice


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 1:43 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
no if you're using Hibernate as persistence it should use Hibernate. which version of Bean Validation are you using?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 1:53 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
s.grinovero wrote:
no if you're using Hibernate as persistence it should use Hibernate. which version of Bean Validation are you using?



here are entries from my pom


Code:
     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.2.0.CR1</version>
     </dependency>

   <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>1.0.0.GA</version>
   </dependency>


and hibernate-core version is 3.5.1-Final


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 2:09 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
could you try replacing the JPA one:
Code:
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
            </dependency>

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 2:55 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
s.grinovero wrote:
could you try replacing the JPA one:
Code:
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
            </dependency>



No difference still the same error. Weblogic by default is assuming two PersistanceProvider providers

here is the code from

Code:
   private static PersistenceUtil util =
         //TODO add an Hibernate specific optimization
      new PersistenceUtil() {
         public boolean isLoaded(Object entity, String attributeName) {
            List<PersistenceProvider> providers = Persistence.getProviders();
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoadedWithoutReference( entity, attributeName );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoadedWithReference( entity, attributeName );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            return true;
         }

         public boolean isLoaded(Object object) {
            List<PersistenceProvider> providers = Persistence.getProviders();
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoaded( object );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            return true;
         }
      };
}



this line List<PersistenceProvider> providers = Persistence.getProviders(); return one implementation of kodo and other implementation of openjpa.


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 3:16 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
fachhoch wrote:
s.grinovero wrote:
could you try replacing the JPA one:
Code:
            <dependency>
                <groupId>org.hibernate.javax.persistence</groupId>
                <artifactId>hibernate-jpa-2.0-api</artifactId>
                <version>1.0.1.Final</version>
            </dependency>



No difference still the same error. Weblogic by default is assuming two PersistanceProvider providers

here is the code from

Code:
   private static PersistenceUtil util =
         //TODO add an Hibernate specific optimization
      new PersistenceUtil() {
         public boolean isLoaded(Object entity, String attributeName) {
            List<PersistenceProvider> providers = Persistence.getProviders();
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoadedWithoutReference( entity, attributeName );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoadedWithReference( entity, attributeName );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            return true;
         }

         public boolean isLoaded(Object object) {
            List<PersistenceProvider> providers = Persistence.getProviders();
            for ( PersistenceProvider provider : providers ) {
               final LoadState state = provider.getProviderUtil().isLoaded( object );
               if ( state == LoadState.UNKNOWN ) continue;
               return state == LoadState.LOADED;
            }
            return true;
         }
      };
}



this line List<PersistenceProvider> providers = Persistence.getProviders(); return one implementation of kodo and other implementation of openjpa.



when I run in jetty the Persistence.getProviders(); return empty arraylist but in case of weblogic I get kodo and openJPA and it fails , please advice me .


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 3:40 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
here is the code from PersistenceProviderResolverHolder from hibernate-jpa-2.0-api-1.0.1.final.jar



Code:

      private static class CachingPersistenceProviderResolver implements PersistenceProviderResolver {
         //this assumes that the class loader keeps the list of classes loaded
         private final List<WeakReference<Class<? extends PersistenceProvider>>> resolverClasses
               = new ArrayList<WeakReference<Class<? extends PersistenceProvider>>>();

         public CachingPersistenceProviderResolver(ClassLoader cl) {
            loadResolverClasses( cl );
         }

         private void loadResolverClasses(ClassLoader cl) {
            synchronized ( resolverClasses ) {
               try {
                  Enumeration<URL> resources = cl.getResources( "META-INF/services/" + PersistenceProvider.class.getName() );
                  Set<String> names = new HashSet<String>();
                  while ( resources.hasMoreElements() ) {
                     URL url = resources.nextElement();
                     InputStream is = url.openStream();
                     try {
                        names.addAll( providerNamesFromReader( new BufferedReader( new InputStreamReader( is ) ) ) );
                     }
                     finally {
                        is.close();
                     }
                  }
                  for ( String s : names ) {
                     @SuppressWarnings( "unchecked" )
                     Class<? extends PersistenceProvider> providerClass = (Class<? extends PersistenceProvider>) cl.loadClass( s );
                     WeakReference<Class<? extends PersistenceProvider>> reference
                           = new WeakReference<Class<? extends PersistenceProvider>>(providerClass);
                     //keep Hibernate atop
                     if ( s.endsWith( "HibernatePersistence" ) && resolverClasses.size() > 0 ) {
                        WeakReference<Class<? extends PersistenceProvider>> movedReference = resolverClasses.get( 0 );
                        resolverClasses.add( 0, reference );
                        resolverClasses.add( movedReference );
                     }
                     else {
                        resolverClasses.add( reference );
                     }
                  }
               }
               catch ( IOException e ) {
                  throw new PersistenceException( e );
               }
               catch ( ClassNotFoundException e ) {
                  throw new PersistenceException( e );
               }
            }
         }

         /**
          * {@inheritDoc}
          */
         public List<PersistenceProvider> getPersistenceProviders() {
            //TODO find a way to cache property instances
            //problem #1: avoid hard ref with classloader (List<WR<PP>>?
            //problem #2: avoid half GC lists
            // todo (steve) : why arent we just caching the PersistenceProvider *instances* as the CachingPersistenceProviderResolver state???
            synchronized ( resolverClasses ) {
               List<PersistenceProvider> providers = new ArrayList<PersistenceProvider>( resolverClasses.size() );
               try {
                  for ( WeakReference<Class<? extends PersistenceProvider>> providerClass : resolverClasses ) {
                     providers.add( providerClass.get().newInstance() );
                  }
               }
               catch ( InstantiationException e ) {
                  throw new PersistenceException( e );
               }
               catch ( IllegalAccessException e ) {
                  throw new PersistenceException( e );
               }
               return providers;
            }
         }

         /**
          * {@inheritDoc}
          */
         public synchronized void clearCachedProviders() {
            synchronized ( resolverClasses ) {
               resolverClasses.clear();
               loadResolverClasses( PersistenceProviderResolverPerClassLoader.getContextualClassLoader() );
            }
         }


         private static final Pattern nonCommentPattern = Pattern.compile( "^([^#]+)" );

         private static Set<String> providerNamesFromReader(BufferedReader reader) throws IOException {
            Set<String> names = new HashSet<String>();
            String line;
            while ( ( line = reader.readLine() ) != null ) {
               line = line.trim();
               Matcher m = nonCommentPattern.matcher( line );
               if ( m.find() ) {
                  names.add( m.group().trim() );
               }
            }
            return names;
         }
      }
   }



this line Enumeration<URL> resources = cl.getResources( "META-INF/services/" + PersistenceProvider.class.getName() );
get the two persistanceproviders , weblogic has openjpa and kodo in its classpath and kodo implenmentation I get classcast exception please advice me how to make hibernate validator work in weblogic


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 4:22 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
I added my own blank impl of PersistanceProviderResolver and it worked , Please advice me if I am doing the right

here is the code

Code:

      PersistenceProviderResolverHolder.setPersistenceProviderResolver(new PersistenceProviderResolver() {
         
         @Override
         public List<PersistenceProvider> getPersistenceProviders() {
            return Lists.newArrayList();
         }
         
         @Override
         public void clearCachedProviders() {
            
         }
      });
      ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
      ValidatorContext validatorContext = factory.usingContext();
      validatorContext.constraintValidatorFactory(new BatchConstraintValidatorFactory(stgAuditGenerals));
      final Validator validator =  validatorContext.getValidator();


will this cause any issue in persistence ?


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 6:19 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 176
Which JPA implementation do you actually want to use? If you want to use Hibernate instead of the JPA provider(s) shipping with WLS you'd have to ensure that the classes provided by Hibernate (JPA API and implementation) are used instead of the ones from WLS. Maybe this blog post helps in doing so.

The original CCE makes me think that PersistenceProvider and PersistenceProviderImpl are loaded by different classloaders.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Wed Jul 06, 2011 8:16 pm 
Newbie

Joined: Mon May 18, 2009 12:00 pm
Posts: 17
I am using only hibernate as orm provider, I am not using JPA,I only use JPA annotations,I don't need JPA provider for persistance, I use hibernate for persistance.I am not using any JPA provider,I am directly using hibernate. Does hibernate validator needs JPA provider?can I use hibernate validator without a JPA provider?


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Thu Jul 07, 2011 2:29 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
fachhoch wrote:
Does hibernate validator needs JPA provider?can I use hibernate validator without a JPA provider?

No the provider is not needed, but if found it will integrate to it.

I agree with Gunnar this looks like a classloading issue, are you having the JPA api jar bundled with the application? that's going to give massive problems as Weblogic likely loaded it already: you should not bundle it and use the one provided by the application server, or if you need to use ours you'll have to configure the classloader isolation on weblogic properly to hide its bundled implementations to the application.

Could you also paste the full stacktrace? I initially assumed that this error was thrown by weblogic's deployer, but since you're not using JPA the error is different.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: deployed to weblogic and I get class cast exception
PostPosted: Thu Jul 07, 2011 6:59 am 
Hibernate Team
Hibernate Team

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

I am tying to make sense of everything here and have a couple of questions.

What was the full stacktrace of your ClassCastException you mentioned in the beginning? Where was this happening?
When you are saying that you are using Hibernate - which dependency to you include? hibernate-entitymanager? Just depending on hibernate-core won't do you any good, unless you really don't want to use JPA. Btw, hibernate-entitymanager defines the javax.persistence.spi.PersistenceProvider service file, so provided you have this dependency in your classpath it should be found by the persistence provider resolution algorithm. This is provided Persistence.getProviders() is really calling into the classes from hibernate-jpa-2.0-api. I would not be surprised if Weblogic uses a different api version. Have you scanned all the jars in your weblogic installation (including shared/common libs)?

Sorry for all these questions, but I am trying to understand what you are trying to achieve. I also find it strange that you only get problems when adding Validator. The only point where Validator checks for JPA is in the TraversableResolver implementation. Hence, I wanted to know where exactly you get a problem. If the TraversableResolver is your problem I recommend setting a custom implementation of this interface.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

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.