Spring找不到类[org.hibernate.ejb.HibernatePersistence] [英] Spring Cannot find class [org.hibernate.ejb.HibernatePersistence]
问题描述
我在eclipse上使用java spring项目,当Im试图通过JUnit测试来运行项目时,我得到和错误:找不到类[org.hibernate.ejb.HibernatePersistence]。我寻找了很多类似的问题,没有人能解决我的问题。我正确地构建和重建项目,但仍然是同样的问题。
首先这是我的JUnit测试:
@Test
public void test(){
try {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
new String [] {applicationContext.xml});
assertTrue(true);
} catch(Exception e){
assertTrue(e.getMessage(),false);
$ b code
$ b 这是错误: p>
org.springframework.context.support.ClassPathXmlApplicationContext - 上下文初始化期间遇到的异常 - 取消刷新尝试:org.springframework.beans.factory。 BeanCreationException:创建在类路径资源[applicationContext.xml]中定义名称为'entityManagerFactory'的bean时出错:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:找不到类[org.hibernate.ejb.HibernatePersistence]
这是我的persistence.xml文件:
<?xml version =1.0encoding =UTF-8?> ;
< persistence version =2.0xmlns =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_2_0.xsd >
< persistence-unit name =GK_SHOPtransaction-type =RESOURCE_LOCAL>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<属性>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =hibernate.dialectvalue =org.hibernate.dialect.MySQLDialect/>
< / properties>
< / persistence-unit>
这是我的弹簧配置file:
<?xml version =1.0encoding =UTF-8?>
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns:context =http://www.springframework.org/schema/context
xmlns:tx =http://www.springframework.org/schema/tx
xsi :schemaLocation =http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http:// www。 springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd\">
< bean id =datasource
class =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver>< / property>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / geek2geek>< / property>
< property name =usernamevalue =aymane>< / property>
< property name =passwordvalue =1234567890>< / property>
< / bean>
< bean id =persistenceUnitManagerclass =org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager>
< property name =defaultDataSourceref =datasource>< / property>
< property name =persistenceXmlLocations>
< list>
< value> classpath *:META-INF / persistence.xml< / value>
< / list>
< / property>
< / bean>
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =persistenceUnitManagerref =persistenceUnitManager/>
< property name =persistenceUnitNamevalue =GK_SHOP/>
< / bean>
< bean id =transactionManager
class =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
这就是maven pom.xml
<?xml version =1.0encoding =UTF-8?>
< 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> com.aymane< / groupId>
< artifactId>商店< / artifactId>
<名称> geekShop< / name>
<包装>战争< / packaging>
< version> 1.0.0-BUILD-SNAPSHOT< / version>
<属性>
< java-version> 1.6< / java-version>
< org.springframework-version> 4.3.7.RELEASE< /org.springframework-version>
< org.aspectj-version> 1.6.10< /org.aspectj-version>
< org.slf4j-version> 1.7.25< /org.slf4j-version>
< / properties>
<依赖关系>
<! - Spring - >
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-context< / artifactId>
< version> $ {org.springframework-version}< / version>
<排除项>
<! - 排除Commons记录SLF4j - >
<排除>
< groupId> commons-logging< / groupId>
< artifactId> commons-logging< / artifactId>
< /排除>
< /排除>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-webmvc< / artifactId>
< version> $ {org.springframework-version}< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-validator< / artifactId>
< version> 5.4.1.Final< / version>
< /依赖关系>
< dependency>
< groupId> org.hibernate< / groupId>
< artifactId> hibernate-entitymanager< / artifactId>
< version> 5.2.10.Final< / version>
< /依赖关系>
< dependency>
< groupId> mysql< / groupId>
< artifactId> mysql-connector-java< / artifactId>
< version> 6.0.6< / version>
< /依赖关系>
<! - - https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api - >
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> javax.servlet-api< / artifactId>
< version> 3.1.0< / version>
< /依赖关系>
<! - AspectJ - >
< dependency>
< groupId> org.aspectj< / groupId>
< artifactId> aspectjrt< / artifactId>
< version> $ {org.aspectj-version}< / version>
< /依赖关系>
<! - 记录 - >
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-api< / artifactId>
< version> $ {org.slf4j-version}< / version>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> jcl-over-slf4j< / artifactId>
< version> $ {org.slf4j-version}< / version>
< scope>运行时< / scope>
< /依赖关系>
< dependency>
< groupId> org.slf4j< / groupId>
< artifactId> slf4j-log4j12< / artifactId>
< version> $ {org.slf4j-version}< / version>
< scope>运行时< / scope>
< /依赖关系>
< dependency>
< groupId> log4j< / groupId>
< artifactId> log4j< / artifactId>
< version> 1.2.15< / version>
<排除项>
<排除>
< groupId> javax.mail< / groupId>
< artifactId>邮件< / artifactId>
< /排除>
<排除>
< groupId> javax.jms< / groupId>
< artifactId> jms< / artifactId>
< /排除>
<排除>
< groupId> com.sun.jdmk< / groupId>
< artifactId> jmxtools< / artifactId>
< /排除>
<排除>
< groupId> com.sun.jmx< / groupId>
< artifactId> jmxri< / artifactId>
< /排除>
< /排除>
< scope>运行时< / scope>
< /依赖关系>
<! - @Inject - >
< dependency>
< groupId> javax.inject< / groupId>
< artifactId> javax.inject< / artifactId>
< version> 1< / version>
< /依赖关系>
<! - Servlet - >
<! - https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api - >
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> javax.servlet-api< / artifactId>
< version> 3.1.0< / version>
< /依赖关系>
<! - - https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api - >
< dependency>
< groupId> javax.servlet.jsp< / groupId>
< artifactId> javax.servlet.jsp-api< / artifactId>
< version> 2.3.1< / version>
< /依赖关系>
< dependency>
< groupId> jstl< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.2< / version>
< scope>编译< / scope>
< /依赖关系>
< dependency>
< groupId> taglibs< / groupId>
< artifactId>标准< / artifactId>
< version> 1.1.2< / version>
< scope>编译< / scope>
< /依赖关系>
< dependency>
< groupId> javax.servlet< / groupId>
< artifactId> jstl< / artifactId>
< version> 1.1.2< / version>
< /依赖关系>
<! - Test - >
< dependency>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.7< / version>
< scope> test< / scope>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-beans< / artifactId>
< version> 4.3.8.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-orm< / artifactId>
< version> 4.3.8.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-core< / artifactId>
< version> 4.3.8.RELEASE< / version>
< /依赖关系>
< dependency>
< groupId> org.springframework< / groupId>
< artifactId> spring-tx< / artifactId>
< version> 4.3.8.RELEASE< / version>
< /依赖关系>
< /依赖关系>
< build>
< plugins>
< plugin>
< artifactId> maven-eclipse-plugin< / artifactId>
< version> 2.9< / version>
<配置>
< additionalProjectnatures>
< projectnature> org.springframework.ide.eclipse.core.springnature< / projectnature>
< / additionalProjectnatures>
< additionalBuildcommands>
< buildcommand> org.springframework.ide.eclipse.core.springbuilder< / buildcommand>
< / additionalBuildcommands>
< downloadSources> true< / downloadSources>
< downloadJavadocs> true< / downloadJavadocs>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 2.5.1< / version>
<配置>
< source> 1.6< / source>
< target> 1.6< / target>
< compilerArgument> -Xlint:all< / compilerArgument>
< showWarnings> true< / showWarnings>
< showDeprecation> true< / showDeprecation>
< / configuration>
< / plugin>
< plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
< version> 1.2.1< / version>
<配置>
< mainClass> org.test.int1.Main< / mainClass>
< / configuration>
< / plugin>
< / plugins>
< / build>
解决方案您应该使用:
org.hibernate.jpa.HibernatePersistenceProvider
而不是
org.hibernate.ejb。 HibernatePersistence
Im working on a java spring project on eclipse, and while Im trying to run the project through a JUnit test, I get and error : Cannot find class [org.hibernate.ejb.HibernatePersistence] . I looked for a lot of similar problems and no one could resolve mine. I build and rebuild the project properly but still the same problem.
First this my JUnit test :
@Test
public void test() {
try {
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
new String[] { "applicationContext.xml" });
assertTrue(true);
} catch (Exception e) {
assertTrue(e.getMessage(), false);
}
}
This is the error :
org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cannot find class [org.hibernate.ejb.HibernatePersistence]
this is my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="GK_SHOP" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
and this is my spring configuration file :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/geek2geek"></property>
<property name="username" value="aymane"></property>
<property name="password" value="1234567890"></property>
</bean>
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="datasource"></property>
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
<property name="persistenceUnitName" value="GK_SHOP" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
and this is the maven pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.aymane</groupId>
<artifactId>shop</artifactId>
<name>geekShop</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.6</java-version>
<org.springframework-version>4.3.7.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.7.25</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Servlet -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<additionalProjectnatures>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
<additionalBuildcommands>
<buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
</additionalBuildcommands>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>org.test.int1.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
解决方案 You should use:
org.hibernate.jpa.HibernatePersistenceProvider
instead of
org.hibernate.ejb.HibernatePersistence
这篇关于Spring找不到类[org.hibernate.ejb.HibernatePersistence]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!