3

大家好,

自从很多天以来我都遇到了这个问题,我能够在 weblogic 10.3.3 上成功部署 JPA2.0 应用程序,该应用程序可以使用 JPA 运行选择查询。但是,当我尝试在同一张表上运行创建或更新信息时,我得到以下异常 [代码]

java.lang.ClassCastException: org.eclipse.persistence.jpa.PersistenceProvider cannot be cast to javax.persistence.spi.PersistenceProvider

[/代码]

这很奇怪,因为在我的 persistence.xml 中,我提供了 hibernate 作为 JPA 提供程序,定义的持久性单元如下 [code]

<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <!-- 

            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
             -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>


            <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <!-- Uncomment the following two properties for JBoss only -->
            <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
            <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
        </properties>
    </persistence-unit>

[/code] 我还在 weblogic.xml 中提供了包级别首选项,下面是我的 weblogic.xml 文件。正如你所看到的,我已经明确告诉 weblogic 更喜欢 web 应用程序包,它仍然试图从 hibernate jar 加载 org.eclipse.persistence.jpa.PersistenceProvider 而不是 persistenceProvider。请帮助我在这里我被困在这一点 [代码]

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 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">
    <wls:weblogic-version>10.3.3</wls:weblogic-version>

    <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>javax.persistence.spi.*</wls:package-name>
            <wls:package-name>javax.persistence.criteria.*</wls:package-name>
            <wls:package-name>javax.persistence.metamodel.*</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>

</wls:weblogic-web-app>

[/代码]

上述异常的整个堆栈跟踪是 [code]

java.lang.ClassCastException: org.eclipse.persistence.jpa.PersistenceProvider cannot be cast to javax.persistence.spi.PersistenceProvider
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:62)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    at com.mycompany.peasd.dbp.entity.DrlgBestPractices.entityManager_aroundBody100(DrlgBestPractices.java:878)
    at com.mycompany.peasd.dbp.entity.DrlgBestPractices.entityManager(DrlgBestPractices.java:1)
    at com.mycompany.peasd.dbp.entity.DrlgBestPractices.findDrlgBestPracticesEntries_aroundBody108(DrlgBestPractices.java:904)
    at com.mycompany.peasd.dbp.entity.DrlgBestPractices.findDrlgBestPracticesEntries(DrlgBestPractices.java:1)
    at com.mycompany.peasd.dbp.web.DrlgBestPracticesController.list(DrlgBestPracticesController.java:669)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
    at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
    at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:300)
    at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at com.mycompany.peasd.dbp.filters.CheckUserAuthorizationFilter.doFilter(CheckUserAuthorizationFilter.java:120)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:147)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.doIt(WebAppServletContext.java:3684)
    at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3650)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
    at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2268)
    at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2174)
    at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1446)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)

[/代码]

我在以下两个论坛上提出了相同的问题以寻求快速帮助。

https://forums.oracle.com/forums/thread.jspa?threadID=2474374&stqc=true https://www.coderanch.com/t/599976/BEA-Weblogic/Weblogic-load-PersistenceProvider-wrong-jar

4

3 回答 3

2

您必须遵循 user1877955 所说的所有内容,并添加此代码,以避免 ClassCastException:

web.xml

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

初始化.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());
    }
}
于 2013-07-09T18:38:54.720 回答
1

以下提示也可能对遇到相同问题的人有所帮助

请注意,为了在符合 JPA1.0 的 weblogic10.3.3 上运行您的 JPA2.0 应用程序,您必须将您的 persistence.xml 重命名为 foo.xml 并在您的 applicationContext.xml 中提及此 xml 文件的名称如(我在这里使用 Spring)

<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>

根据您在应用程序中定义的 bean 重命名 persistenceUnit 和 dataSource

并且您必须在 weblogic.xml 文件中将包排除定义为

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>

我花了几天时间来解决这个问题,并分享解决方案,希望有一天它可以使某人受益,欢呼。

请在我下面的博客文章中找到所有其他详细信息

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

于 2012-12-16T05:51:24.227 回答
0

我终于找到了解决这个问题的方法,现在 JPA2.0 应用程序在 Weblogic10.3.3 服务器上运行良好。

我发现问题实际上与休眠验证器有关,在我的 JPA 方法代码中,我有一个“@Valid”注释以及我得到的对象,我试图在持久化之前使用休眠验证器来验证对象,那里我得到了所有这些例外。为了让我的应用程序在服务器上运行,我必须从我的 JPA 代码中删除那些 @valid 注释,现在一切正常。但现在我依赖于页面级别的验证,这在我的情况下已经足够好了。然而,理想的解决方案是找到一个兼容的 hibernat 验证器 jar 并将 @valid 注释保留在您的项目中,以便更好地验证。现在这个解决方案对我有用,ASA 我有时间我会尝试找到可以在我们拥有的服务器上使用此版本的休眠验证器 jar,并且也希望进行服务器端验证。我希望这种解决方法可以节省一些人的时间:)。

于 2012-12-16T05:38:57.663 回答