Hibernate java.lang.NoClassDefFoundError:org / hibernate / util / DTDEntityResolver

时间:2012-08-03 22:37:37

标签: java spring hibernate

我对休眠很新, 而我只是想让我的第一个程序使用它。

我正在使用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>

然后我创建了hibernate配置文件:

<?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)

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

好的,我自己找到了一个解决方案。

有几个错误。 pom中的冷杉我删除了这种依赖:

   <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-tools</artifactId>
<version>3.2.4.GA</version>

然后我收到另一个错误:找不到hibernate.cfg.xml,我环顾四周,发现这个配置文件必须放在java源的根文件夹中(所以当部署应用程序时它会放到classes文件夹中) 。

最后我还有另一个。在转换为sessionFactory的声明时,我将其声明为Configuration变量,但它必须是SessionFactory变量。

最后,映射存在另一个问题(Project类的错误包)。

它似乎有效:)