我正在尝试使用maven运行一个基本的hibernate 5程序。它在创建Configuration对象时出错。我错过了什么?得到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at org.spade.utility.HibernateUtility.getSessionFactory(HibernateUtility.java:15)
at org.spade.dao.UserDao.saveUser(UserDao.java:21)
at org.spade.dao.UserDao.main(UserDao.java:12)
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
这是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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.spade</groupId>
<artifactId>HibernateDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>com.github.noraui</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我已经看到SO的其他答案为同样的问题提供解决方案,但没有一个有效。
答案 0 :(得分:0)
你是在像jboss这样的服务器上运行吗?如果是这样,它可能是hibernate的不匹配版本。 尝试添加提供的范围以使用服务器版本,如下所示:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
<scope>provided</scope>
</dependency>