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.ziptomcat 5.0.28Http: //apache.linuxForum.net/dist/ JAKARTA / TOMCAT-5 / V5.0.28 / bin / jakarta-tomcat-5.0.28.zipHibernate2.1.6http: //prdownloads.sourceForge.Net/Hibernate/? sort_by = Date & Sort = DESC
2. Installation
Take Tomcat MySQL Hibernate as an example
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 it is assumed that tomcat mysql has been configured And test, here% webapp% represents root of a web application you have configured, focusing on how Hibernate installation,
1) Unzip the downloaded compressed package, copy the Hibernate2.jar to% WebApp% / WEB-INF / LIB
2) will decompress the LIB directory
CGLIB-FULL-2.0.2.jarcommons-Collections-2.1.1.jarcommons-logging-1.0.4.jardom4j-1.4.jarehcache-0.9.jarjta.jarlog4j-1.2.8.jarodmg-3.0.jar
Documents are also copied to% WebApp% / Web-INF / LIB. For details, see README.TXT under the decompressed LIB directory. 3) will decompress the ETC directory
Log4j.properties
Documents are copied to% webApp% / web-inf / class.
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'?>
2) Save the following code as Cat.java, and generate corresponding Cat.class, put% WebApp% / Web-INF / CLASSES, where you use what method to generate Cat.class, but final Cat.Class should be% WebApp% / Web-INF / CLASSES / NET / SF / Hibernate / Examples / QuickStart directory
package net.sf.hibernate.examples.quickstart; public class Cat {private String id; private String name; private char sex; private float weight; public Cat () {} public String getId () {return id;} private void setId (String 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;}}
3) Save the following code as an O / R mapping file Cat.hbm.xml, put% WebApp% / Web-INF / CLASSES
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) Construction form 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
<% @ page language = "java" PageEncoding = "GB2312"%> <% @ page import = "net.sf.hibernate.Transaction"%> <% @ page import = "net.sf.hibernate.Session"%> <% @ page import = "Net.sf.hibernate.cfg. *"%> <% @ page import = "net.sf.hibernate.query"%> <% @ page import = "net.sf.hibernate.examples .quickstart.hibernateutil "%> <% @ page import =" net.sf.hibernate.examples.quickstart.cat "%> <% @ Page Import =" java.util. * "%>
The steps are basically the same as the original document step, just have some additions, convenient to play, there is no specific explanation for some of these configurations, please refer to the related document in the release package if you have any questions.
The above Tomcat5.027 Hibernate2.1.6 test passed
Attachment:
Tomcat official website http://jakarta.apache.org/tomcat/index.htmlhib: //www.hibernate.orghibernamte unofficial Chinese network http://www.hibernate.org.cnmysql official graphics management tool graphical Clients - DiffERENT GUI Interfaces To Administer MySQL AND DATAMYSQL Administrator MySQL Query Browser MySQL Control Center (No Longer Under Development)