我对休眠很陌生,我只是想制作我的第一个使用它的程序。
我正在使用 Maven 来构建项目。我声明了这些依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.5.SP1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.5.SP1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.4.GA</version>
</dependency>
然后我创建了休眠配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">******</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/vcsdb</property>
<property name="hibernate.connection.username">vcsuser</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping class="org.test.database.Project"/>
</session-factory
</hibernate-configuration>
然后我创建了一个逆向工程文件:
<?xml version="1.0" encoding="UTF-8"?>
使用 eclipse hibernate 插件,我创建了代表数据库中唯一表的实体:
package org.test.database;
// Generated Jul 28, 2012 4:34:03 PM by Hibernate Tools 3.4.0.CR1
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* Project generated by hbm2java
*/
@Entity
@Table(name = "project", catalog = "versioncontroldb")
public class Project implements java.io.Serializable {
private Integer id;
private String name;
private String longName;
private String siteUrl;
private String projectUrl;
private Date registered;
private String description;
private String userId;
public Project() {
}
public Project(String name, String projectUrl, Date registered) {
this.name = name;
this.projectUrl = projectUrl;
this.registered = registered;
}
public Project(String name, String longName, String siteUrl,
String projectUrl, Date registered, String description,
String userId) {
this.name = name;
this.longName = longName;
this.siteUrl = siteUrl;
this.projectUrl = projectUrl;
this.registered = registered;
this.description = description;
this.userId = userId;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "name", nullable = false, length = 30)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "long_name", length = 65535)
public String getLongName() {
return this.longName;
}
public void setLongName(String longName) {
this.longName = longName;
}
@Column(name = "site_url", length = 150)
public String getSiteUrl() {
return this.siteUrl;
}
public void setSiteUrl(String siteUrl) {
this.siteUrl = siteUrl;
}
@Column(name = "project_url", nullable = false, length = 200)
public String getProjectUrl() {
return this.projectUrl;
}
public void setProjectUrl(String projectUrl) {
this.projectUrl = projectUrl;
}
@Temporal(TemporalType.DATE)
@Column(name = "registered", nullable = false, length = 10)
public Date getRegistered() {
return this.registered;
}
public void setRegistered(Date registered) {
this.registered = registered;
}
@Column(name = "description", length = 65535)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "user_id", length = 25)
public String getUserId() {
return this.userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
最后,我将以下代码行添加到应用程序的主要 servlet 类中:
Configuration sessionFactory = (Configuration) new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
但是当我尝试启动应用程序时,我收到以下错误消息:
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
和:
java.lang.NoClassDefFoundError: org/hibernate/util/DTDEntityResolver
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:791)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2901)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1170)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)
org.hibernate.cfg.Configuration.reset(Configuration.java:326)
org.hibernate.cfg.Configuration.<init>(Configuration.java:265)
org.hibernate.cfg.Configuration.<init>(Configuration.java:269)
org.versioncontrolanalyzer.controllers.MainController.helloWorld(MainController.java:18)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
任何的想法?