我想在Oracle DB上使用Spring 3.2.8 + JPA / Hibernate(对我来说是第一次)。
我认为我无法正确配置(持久性单元,spring配置文件),因此在Tomcat中部署时总是出错。
Caused by: java.lang.ClassNotFoundException: org.Hibernate.ejb.HibernatePersistence
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:255)
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:288)
... 38 more
Aug 08, 2014 1:37:16 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Aug 08, 2014 1:37:16 AM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/ftc-wo-webapp] startup failed due to previous errors
Aug 08, 2014 1:37:16 AM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Aug 08, 2014 1:37:16 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
hibernate jar(包括实体管理器jar)包含在最后的战争中(在WEB-INF / lib下)所以我不明白为什么这个类还没有建立。 存在以下配置:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="woBPFlowInfo">
<provider>org.Hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
main spring配置文件:app_config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
...
<context:component-scan base-package="com.ftc.webapp" />
<mvc:annotation-driven/>
<!-- Database added -->
<!-- Loads BPFlowInfo from the JPA system -->
<bean id="woBPFlowRepository" class="com.ftc.webapp.data.JpaWoBPFlowRepository"/>
<!-- Instructs the container to look for beans with @Transactional and decorate them -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Enables annotation-config, needed for @PersistenceContext annotations -->
<context:annotation-config />
<import resource="db_config.xml"/>
</beans>
db_config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- // NAME: rewards-db-config.xml // // MODULE: rewardsdb // // PURPOSE:
Database and transaction configuration for testing the rewards // application. -->
<beans xmlns="...">
<context:annotation-config />
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="testOnBorrow" value="true" />
<property name="initialSize" value="2" />
<property name="maxActive" value="50" />
<property name="removeAbandoned" value="true"/>
<property name="removeAbandonedTimeout" value="300"/>
<property name="validationQuery" value="select 1 from dual" />
</bean>
<!-- Translates ORM exceptions to Spring Data Access Exceptions -->
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<!-- Data access using 100% JPA -->
<!-- A transaction manager for working with JPA Entity Managers -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- FactoryBean that creates the EntityManagerFactory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="woBPFlowInfo" />
</bean>
</beans>
和pom.xml包含以下依赖项:
的pom.xml
<properties>
<smp.version>5.2.2.6</smp.version>
<spring.version>3.2.8.RELEASE</spring.version>
<hibernate.version>3.4.0.GA</hibernate.version>
<javaxServlet.version>2.5</javaxServlet.version>
<jackson.version>1.9.5</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>${javaxServlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- Persistency & Object Relational mapping -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>ojdbc</groupId>
<artifactId>ojdbc</artifactId>
<version>14</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
答案 0 :(得分:2)
我怀疑 - 配置错误,实际上拼写错误。 取代
<provider>org.Hibernate.ejb.HibernatePersistence</provider>
与
<provider>org.hibernate.ejb.HibernatePersistence</provider>
和ClassNotFoundException:org.Hibernate.ejb.HibernatePersistence消失
足够愚蠢。我为这个愚蠢的问题花了很多时间。 遗憾。
答案 1 :(得分:0)
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
是正确的持久性提供程序。
<provider>org.hibernate.ejb.HibernatePersistence</provider>
已被弃用。