Hibernate first experience Cat quick start

xiaoxiao2021-03-06  70

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

java: comp / env / jdbc / mysql false Net.sf.hibernate.diaalect.mysqldiaalect Note: Here is different from the Hibernate document, here specifying the data source is mysql database JDBC / MySQL, dialect DiaLect is Net.sf.hibernate.Dialect.MysqldiaAlact. 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 / directory under quickstart 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 set;} 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 the O / R mapping file Cat.hbm.xml, put% WebApp% / WEB-INF / CLASSES

Public "- // hibernate / hibernate mapping dtd // en"

"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

Generated by hibernate with the uuid pattern. ->

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

转载请注明原文地址:https://www.9cbs.com/read-120359.html

New Post(0)