Hibernate's CAT example feels unktown, it is difficult to run the first example with it, take a point of discount, I finally see a result, if you are a newbie, you can refer to it, less detours . 1. Download Tomcat and Hibernate
Tomcat 5.0.27http: //apache.linuxForum.Net/dist/jakarta/tomcat-5/v5.0.27/bin/jakarta-tomcat-5.0.27.zip Tomcat 5.0.28Http: //apache.linuxForum.Net/dist /jakarta/tomcat-5/v5.0.28/bin/jakarta-tomcat-5.0.28.zip hibernate2.1.6http: //prdownloads.SourceForge.Net/Hibernate/? sort_by = DATE & SORT = DESC2. Installation Tomcat MySQL Hibernate as an example of Tomcat installation, and Mysql installation and DBCP formulation see http://blog.9cbs.net/ahxu/archive/2004/09/01/91611.aspx, here is not mentioned, here to assume tomcat MySQL has been configured and tested, here% WebApp% represents root of a web app you have configured, focusing on hibernate installation, 1) Unzip the downloaded compressed package, will decompress the hibernate2.jar to% WebAPPP% / WEB-INF / LIB 2) will decompress the CGLIB-Full-2.0.2.jarcommons-Collections-2.0.4.jarcommons-Logging-1.0.4.jarcommons-Logging-1.0.4.jarcommons-12.jarehcache-0.9.jarjta under the LIB directory. Jarlog4j-1.2.8.jarodmg-3.0.jar files are also copied to% WebApp% / Web-INF / LIB. For details, see README.TXT under the decompressed LIB directory. 3) Copy the log4j.properties file under the decompressed ETC directory to% WebApp% / Web-INF / CLASSES. 3. Write related files 1) Follow the reference documents, configure Hibernate, save the following code as hibernate.cfg.xml placed under the% WebApp% / Web-INF / CLASSES
Xml version = '1.0' encoding = 'utf-8'?>
XML Version = "1.0"?>
Public "- // hibernate / hibernate mapping dtd // en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
Generated by hibernate with the uuid pattern. ->
id>
Property>
clas>
hibernate-mapping 4) Built in the database, the structure is as follows
Column | Type | Modifier
-------- ---------------------- -----------
CAT_ID | Character (32) | NOT NULL
Name | Character Varying (16) | NOT NULL
SEX | Character (1) |
Weight | Real |
Indexes: CAT_PKEY PRIMARY Key Btree (CAT_ID) 5) Save the following code as hibernateutil.java, and generate the corresponding hibernateutil.class, put% WebApp% / Web-INF / CLASSES, also pay attention to Package
Package net.sf.hibernate.examples.quickstart;
Import org.apache.commons.logging.log; import org.apache.commons.logging.logfactory;
Import net.sf.hibernate. *;
import net.sf.hibernate.cfg *;. public class HibernateUtil {private static Log log = LogFactory.getLog (HibernateUtil.class); private static final SessionFactory sessionFactory; static {try {// Create the SessionFactorysessionFactory = new Configuration (). . configure () buildSessionFactory ();} catch (Throwable ex) {log.error ( "Initial SessionFactory creation failed.", ex); throw new ExceptionInInitializerError (ex);}} public static final ThreadLocal session = new ThreadLocal (); Public static session currentations () throws hibernateException {session s = (session) session.get (); // Open a new session, if this thread has none yetif (s == null) {s = sessionFactory.openSession (); session .SET (s);} return s;} shRows HibernateException {session s = (session) session.get (); session.set (null); if (s! = null) S.Close );}} Note: Here is also different from the Hibernate document. There are 2 packages that are missing when the original document is completed. 6) Save the following code as Test.jsp, put% WebApp% / with HTTP test