2

I'm very new to hibernate, and i'm simply trying to make my first program that use it.

I'm using maven for building the project. I declared these dependencies:

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

Then i created the hibernate configuration file:

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

Then i created a reverse enginering file:

<?xml version="1.0" encoding="UTF-8"?>

Using eclipse hibernate plugin, i created the entity that represent the only table present in the database:

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;
    }

}

And finally i added into main servlet classs for the application the follwing line of code:

Configuration sessionFactory = (Configuration) new Configuration()
    .configure() // configures settings from hibernate.cfg.xml
    .buildSessionFactory();

But when i try to launch the application i receive the following error message:

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)

and:

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)

Any idea?

1 Answer 1

2

Ok i found a solution myself.

There was several errors. Firs in the pom i removed this dependency:

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

Then i received another error: Cannot find hibernate.cfg.xml, i looked around and i found that this configuration file must be placed into root folder of java sources (so it will places onto classes folder when the application is deployed).

Finally i had another . of casting into declaration of sessionFactory, i declared it as Configuration variable, but it must be a SessionFactory Variable.

Finally there is another problem with the mapping (wrong package for the Project class).

And it seems to works :)

Sign up to request clarification or add additional context in comments.

1 Comment

If you follow Maven Standard Directory Layout it's better to place your hibernate.cfg.xml in src/main/resources instead of src/main/java

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.