Weblogic 10.3.3 trying to load org.eclipse.persistence.jpa.PersistenceProvider instead of configured Hibernate Provider

和自甴很熟 提交于 2019-11-30 15:45:09

You have to follow all that user1877955 said, and add this code, to avoid ClassCastException:

web.xml

<listener>
    <listener-class>com.bla.Init</listener-class>
</listener>

Init.java

package com.bla;

import javax.servlet.ServletContextEvent;

public class Initjavax.servlet.ServletContextListener {

    public void contextDestroyed(ServletContextEvent arg0) {

    }

    public void contextInitialized(ServletContextEvent arg0) {
        HibernatePersistenceProviderResolver.register();
    }
}

HibernatePersistenceProviderResolver.java

package com.bla;

import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import javax.persistence.spi.PersistenceProvider;
import javax.persistence.spi.PersistenceProviderResolver;
import javax.persistence.spi.PersistenceProviderResolverHolder;

import org.hibernate.ejb.HibernatePersistence;

public class HibernatePersistenceProviderResolver implements PersistenceProviderResolver {
    private static final Logger LOGGER = Logger.getLogger(HibernatePersistenceProviderResolver.class.getName());

    private volatile PersistenceProvider persistenceProvider = new HibernatePersistence();

    public List<PersistenceProvider> getPersistenceProviders() {
        return Collections.singletonList(persistenceProvider);
    }

    public void clearCachedProviders() {
        persistenceProvider = new HibernatePersistence();
    }

    public static void register() {
        LOGGER.info("Registering HibernatePersistenceProviderResolver");
        PersistenceProviderResolverHolder.setPersistenceProviderResolver(new HibernatePersistenceProviderResolver());
    }
}

Below tip may also be helpfull for someone having the same problem

Please note that in order to run your JPA2.0 application on weblogic10.3.3 which is JPA1.0 compliant you will have to rename your persistence.xml to something like foo.xml and mentione the name of this xml file in your applicationContext.xml as (I am using Spring here )

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property value="classpath:META-INF/foo.xml" name="persistenceXmlLocation"/>
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

rename the persistenceUnit and dataSource according to the beans you have defined in your application

and you will have to define package exclusions in your weblogic.xml file as

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd"> 10.3.3

<wls:container-descriptor>
    <wls:index-directory-enabled>false</wls:index-directory-enabled>
    <!-- prefer-application-packages> <package-name>javax.persistence.spi.*</package-name> 
        </prefer-application-packages -->
    <wls:prefer-application-packages>
        <wls:package-name>antlr.*</wls:package-name>
        <wls:package-name>org.apache.commons.*</wls:package-name>
        <wls:package-name>org.apache.xmlbeans.*</wls:package-name>
        <wls:package-name>org.springframework.*</wls:package-name>
        <wls:package-name>org.hibernate.*</wls:package-name>

        <wls:package-name>org.hibernate.validator.*</wls:package-name>

        <wls:package-name>javax.persistence.*</wls:package-name>
        <wls:package-name>org.joda.*</wls:package-name>
    </wls:prefer-application-packages>
</wls:container-descriptor>

I invested few days to resolve the problem , and sharing the solution hoping it might benefit someone someday cheers.

Please find all the other details on my below blog post

http://javaiscoool.blogspot.com/2012/12/deploy-jpa20-application-on-weblogic1033.html

I finally figured a workaround for this problem , and now JPA2.0 application is running fine on Weblogic10.3.3 server.

I found out that the problem was actully relted to hibernate validator , In my JPA method code I had a "@Valid" annotation along with the object I was getting , I was trying to use hibernate validator to validate the object before persisting it , there I was getting all these exceptions. To get my application running on server I had to remove those @valid annotations from my JPA code and now everything is working fine. But now I am relying on page level validation which in my case is good enough. However the ideal solution would be to find a compatable hibernat validator jar and keep the @valid annotations in your project for the sake of better validation. For now this solution is working for me , ASA I get time I will try to find the hibernate validator jar which can work with this version on server we have and will love to have server side validation as well. I hope this workaround will save some days of someone out there :).

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