I have been unable to run the simple example below. It is a servlet which should update a mysql database with a table called cat. The table fields are
cat_id, int
name, varchar
sex, char
weight, float
The servlet is as follows:
Code:
...
import net.sf.hibernate.Session;
import java.io.PrintWriter;
public class UpdateServlet extends HttpServlet {
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
SessionFactory sessionFactory = null;
Session session = null;
Transaction transaction = null;
Configuration cfg = null;
try {
PrintWriter out = response.getWriter();
out.println("inside update servlet");
cfg = new Configuration().configure(); // error line 31 here
// also tried ff also error
// cfg = new Configuration()
// .addClass(net.sf.hibernate.testwebapp.quickstart.Cat.class);
sessionFactory = cfg.buildSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Cat domestic = new Cat();
domestic.setName("lingoName");
domestic.setSex('M');
domestic.setWeight(123.0F);
session.save(domestic);
transaction.commit();
session.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
The Cat class is as follows
Code:
package net.sf.hibernate.testwebapp.quickstart;
public class Cat {
private int id;
private String name;
private char sex;
private float weight;
public Cat() {}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public char getSex() { return sex; }
public void setSex(char sex) { this.sex = sex; }
public float getWeight() { return weight; }
public void setWeight(float weight) { this.weight = weight; }
}
My Cat.hbm.xml file (which is in the same folder as my Cat.class) is as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="net.sf.hibernate.testwebapp.quickstart.Cat" table="cat">
<id name="id" type="int" column="cat_id" >
<generator class="native" />
</id>
<property name="name"/>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
I also tried generator class="identity" & "sequence" above, still didnt run.
My hibernate.cfg.xml (in WEB-INF\classes) is as follows
Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory name="java:comp/env/hibernate/SessionFactory">
<property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files -->
<mapping resource="/net/sf/hibernate/testwebapp/quickstart/Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Kindly explain why the following error shows in the browser when I run the servlet
Code:
exception
javax.servlet.ServletException: Servlet execution threw an exception
...
root cause
java.lang.ExceptionInInitializerError
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:627)
at net.sf.hibernate.testwebapp.quickstart.UpdateServlet.doPost(UpdateServlet.java:31)
...
The following lines are at the end of the log file.
Code:
...
INFO: Mapping resource: /net/sf/hibernate/testwebapp/quickstart/Cat.hbm.xml
Nov 27, 2003 7:21:33 PM net.sf.hibernate.cfg.Binder bindRootClass
INFO: Mapping class: net.sf.hibernate.testwebapp.quickstart.Cat -> cat
Nov 27, 2003 7:21:34 PM net.sf.hibernate.cfg.Configuration configure
INFO: Configured SessionFactory: java:comp/env/hibernate/SessionFactory
Nov 27, 2003 7:21:34 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing one-to-many association mappings
Nov 27, 2003 7:21:34 PM net.sf.hibernate.cfg.Configuration secondPassCompile
INFO: processing foreign key constraints
Thanks for any help.