Hibernate version:
3.0
hibernate.cfg.xml:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Settings for a local HSQL (testing) database. -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/dbname</property>
<property name="connection.username">root</property>
<property name="connection.password">password</property>
<!-- Use the C3P0 connection pool. -->
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">1800</property>
<!-- Disable second-level cache. -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="max_fetch_depth">3</property>
<!-- Print SQL to stdout. -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and then re-create schema on SessionFactory build, for testing. -->
<property name="hbm2ddl.auto">create</property>
<!-- Bind the getCurrentSession() method to the thread. -->
<property name="current_session_context_class">thread</property>
<!-- Hibernate XML mapping files -->
<mapping resource="test/TestDAO.hbm.xml"/>
</session-factory>
</hibernate-configuration>
TestDAO.hbm.xml:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="TestDAO" table="TST_TestDAO">
<id name="id" column="testDAOID" type="long">
<generator class="native" />
</id>
<property name="testParam" type="integer" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():TestServletCode:
public class TestServlet extends HttpServlet{
private static final long serialVersionUID = -4475599757796846392L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
TestDAO testDAO = new TestDAO();
testDAO.setTestParam(5);
session.save(testDAO);
session.close();
}
}
TestDAO.javaCode:
package test;
public class TestDAO {
private Long id = null;
private int testParam;
public TestDAO(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public int getTestParam() {
return testParam;
}
public void setTestParam(int testParam) {
this.testParam = testParam;
}
}
HibernateUtilCode:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Full stack trace of any exception that occurs:Code:
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
business_logic.HibernateUtil.<clinit>(HibernateUtil.java:17)
business_logic.servlets.login.Login.processRequest(Login.java:42)
business_logic.servlets.BaseServlet.doPost(BaseServlet.java:42)
business_logic.servlets.BaseServlet.doGet(BaseServlet.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
org.hibernate.MappingException: entity class not found: TestDAO
org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:99)
org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:168)
org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:124)
org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
business_logic.HibernateUtil.<clinit>(HibernateUtil.java:13)
business_logic.servlets.login.Login.processRequest(Login.java:42)
business_logic.servlets.BaseServlet.doPost(BaseServlet.java:42)
business_logic.servlets.BaseServlet.doGet(BaseServlet.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
java.lang.ClassNotFoundException: TestDAO
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Unknown Source)
org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:96)
org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:168)
org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:44)
org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:124)
org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
business_logic.HibernateUtil.<clinit>(HibernateUtil.java:13)
business_logic.servlets.login.Login.processRequest(Login.java:42)
business_logic.servlets.BaseServlet.doPost(BaseServlet.java:42)
business_logic.servlets.BaseServlet.doGet(BaseServlet.java:25)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
Name and version of the database you are using:
MySQL 5.0
This exception is thrown in my HibernateUtil class when I try to create the SessionFactory object in the static block. I am able to run hbm2ddl to generate my database schema so I'm not sure why Hibernate can't find TestDAO when I try to get the SessionFactory. Any ideas?