I create the webapps according to the reference of hibernate of version 3.0.5.
In tomcat webapps i create the directory of quickstart who is just like this:
+src
+org
+hibernate
+examples
+quickstart
-Cat.java
-HibernateUtil.java
-TestHServlet.java (a servlet )
+WEB-INF
+classes
+org
-Cat.hbm.xml
-hibernate.cfg.xml
-log4j.properties
+hibernate
+examples
+quickstart
-Cat.class
-HibernateUtil.class
-TestHServlet.class
+lib
-web.xml
in tomcat ,under the directory of conf\server.xml ,i added the Context describing the JNDI
<Context path="/quickstart" docBase="quickstart">
<Resource name="jdbc/quickstart" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/quickstart">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/quickstart</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>username</name>
<name>root</name>
</parameter>
<parameter>
<name>password</name>
<value>dfjk</value>
</parameter>
<!--DBCP connection pooling options-->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>
the context of web.xml is
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xdd"
version="2.4">
<display-name>Hibernate connect the DB with Tomcat </display-name>
<description>
Persistence layer using Hibernate
</description>
<servlet>
<servlet-name>TestHServlet</servlet-name>
<servlet-class>org.hibernate.examples.quickstart.TestHServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestHServlet</servlet-name>
<url-pattern>/HibernateQuickstart</url-pattern>
</servlet-mapping>
<resource-ref>
<description>DB connection</description>
<res-ref-name>jdbc/quickstart</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
the main content of hibernat.cfg.xml is
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="Cat.hbm.xml"/>
</session-factory>
</hibernate-configuration>
the configuration the Cat.hbm.xml is
<hibernate-mapping>
<class name="org.hibernate.examples.quickstart.Cat" table="CAT">
<id name="id" type="string" unsaved-value="null">
<column name="CAT_ID" not-null="true"/>
<generator class="generator"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" length="16" not-null="true"/>
</property>
<property name="sex"/>
<property name="weight"/>
</class>
</hibernate-mapping>
when i keypress
http://localhost:8080/quickstart/HibernateQuickstart ,there errors generated as following:
exception
javax.servlet.ServletException: Servlet execution threw an exception
root cause
java.lang.ExceptionInInitializerError
org.hibernate.examples.quickstart.HibernateUtil.<clinit>(HibernateUtil.java:18)
org.hibernate.examples.quickstart.TestHServlet.doGet(TestHServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
How to deal with it ,3X
gabendou