2

我正在使用 Hibernate 4、Spring 3、JSF 2.0 和 Weblogic 10.3.6

当我启动 weblogic 服务器并且服务器成功启动时,但是每当它开始发布应用程序时,我都会收到以下异常。

   Error creating bean with name 
 'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0' 
  defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
  Initialization of bean failed; nested exception is 
  org.springframework.beans.factory.BeanCreationException: 

    Caused By: org.springframework.beans.factory.BeanCreationException: Error creating 
   bean with name 'entityManagerFactory'defined in ServletContext resource 
   [/WEB-INF/applicationContext.xml]: Invocation of init method failed; 
   nested exception is java.lang.NoSuchMethodError: 
   javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode()Ljavax/persistence/
   SharedCacheMode;

Caused By: java.lang.NoSuchMethodError: 
javax.persistence.spi.PersistenceUnitInfo.getSharedCacheMode()
Ljavax/persistence /SharedCacheMode;    at 
org.hibernate.ejb.util.LogHelper.logPersistenceUnitInfo(LogHelper.java:38)  at
org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:525)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:72)     at  org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManage

我在 applicationContext.xml 中有以下内容

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">      
    <context:component-scan base-package="net.test" />
    <!-- Data Source Declaration -->
    <bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass" value="oracle.jdbc" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@server:1521:DB" />
        <property name="user" value="scott" />
        <property name="password" value="tiger" />
        <property name="maxPoolSize" value="10" />
        <property name="maxStatements" value="0" />
        <property name="minPoolSize" value="5" />
    </bean>
    <bean
        class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <!-- JPA Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="packagesToScan" value="net.test.entity" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="generateDdl" value="false" />
                <property name="databasePlatform" value="${jdbc.dialectClass}" />

            </bean>
        </property>
    </bean>
    <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
    <!-- Session Factory Declaration -->
    <bean id="SessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="DataSource" />
        <property name="annotatedClasses">
            <list>
                <value>net.test.entity.Department</value>
                <value>net.test.entity.Employees</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
                </prop>
            </props>
        </property>
    </bean>
    <!-- Enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager" />
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- Transaction Config -->

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="SessionFactory" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <context:annotation-config />    

    <bean id="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService">
        <property name="statisticsEnabled" value="true" />
        <property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" />
    </bean>
    <bean name="ehCacheManagerMBean"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
        <property name="locateExistingServerIfPossible" value="true" />
    </bean>    
    <bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter"
        lazy-init="false">
        <property name="server" ref="mbeanServer" />
        <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING" />
        <property name="beans">
            <map>
                <entry key="SpringBeans:name=hibernateStatisticsMBean"
                    value-ref="hibernateStatisticsMBean" />
                <entry key="SpringBeans:name=ehCacheManagerMBean" value-ref="ehCacheManagerMBean" />
            </map>
        </property>
    </bean>

</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
          http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.test</groupId>
    <artifactId>myappp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version> 
    <name>myappp</name> 
    <url>http://maven.apache.org</url>
    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>PrimeFaces Maven Repository</name>
            <url>http://repository.primefaces.org</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <properties>
        <spring.version>3.1.1.RELEASE</spring.version>
    </properties>
    <dependencies>
        <!-- Spring 3 dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- JSF library -->
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Primefaces library -->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>afterwork</artifactId>
            <version>1.0.8</version>
        </dependency>
        <!-- Hibernate library -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <!-- Oracle Java Connector library -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- Log4j library -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>1.2.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
    <groupId>org.apache.myfaces.extensions.cdi.core</groupId>
    <artifactId>myfaces-extcdi-core-api</artifactId>
    <version>1.0.5</version>
    <scope>compile</scope>
</dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.0.1.Final</version>
        </dependency>       
    </dependencies> 
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>              
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-proc:none</compilerArgument>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.bsc.maven</groupId>
                <artifactId>maven-processor-plugin</artifactId>
                <version>2.0.6</version>
                <executions>
                    <execution>
                        <id>process</id>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <!-- source output directory -->
                            <outputDirectory>target/metamodel</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>

在 Weblogic 10.3.6 中,我通过添加以下内容启用了 JPA2 支持commEnv.cmd

@rem Enable JPA 2.0 functionality on WebLogic Server 
set PRE_CLASSPATH=%BEA_HOME%\modules\javax.persistence_1.1.0.0_2-0.jar;
%BEA_HOME%\modules\com.oracle.jpa2support_1.0.0.0_2-1.jar

如何解决此错误?

4

2 回答 2

1

这个问题已经解决了,对于Weblogic 10.3.6 exe发行版没有javax.persistence_1.0.0.0_2-0-0.jar,有的是javax.persistence_1.1.0.0_2-0.jar.

该类以及jpa2support_1.0.0.0_2-0.jar需要在类路径中。

谢谢

于 2013-01-29T17:13:49.697 回答
0

也许问题与类加载层次结构有关,并且某些类是从系统类加载器而不是 EAR 中包含的 jar 加载的。

WebLogic Server 中,系统类路径中存在的任何 .jar 文件都由 WebLogic Server系统类加载器加载。在服务器实例中运行的所有应用程序都加载到应用程序类加载器中,这些类加载器是系统类加载器的子级。在系统类加载器的这个实现中,应用程序不能使用系统类加载器中已经存在的不同版本的第三方 jar。每个子类加载器都向父类(系统类加载器)询问特定类,并且无法加载父类看到的类。

例如,如果一个名为 com.foo.Baz 的类同时存在于 $CLASSPATH 和应用程序 EAR 中,则加载来自 $CLASSPATH 的类而不是来自 EAR 的类。由于 weblogic.jar 在 $CLASSPATH 中,应用程序不能覆盖任何 WebLogic Server 类。

FilteringClassLoader为您提供了一种机制来配置部署描述符,以明确指定应始终从应用程序加载某些包,而不是由系统类加载器加载。

FilteringClassLoader位于应用程序类加载器和系统之间。它是系统类加载器的子类和应用程序类加载器的父类。FilteringClassLoader 拦截 loadClass(String className) 方法并将 className 与weblogic-application.xml文件中指定的包列表进行比较。

总之,您是否会尝试修改您的 weblogic-application.xml 以从 EAR 中包含的 jar 而不是系统类加载器中加载一些包?一个示例示例如下:

<prefer-application-packages>
    <package-name>antlr.*</package-name>
    <package-name>org.apache.commons.*</package-name>
    <package-name>org.apache.xmlbeans.*</package-name>
    <package-name>org.springframework.*</package-name>
    <package-name>org.hibernate.*</package-name>
    <package-name>javax.persistence.*</package-name>
</prefer-application-packages>
于 2013-01-28T13:54:47.197 回答