JBuilder Hibernate Tomcat Quick Start
Gaoke Hua
About the author: Gao Jinghua, Nanjing Aviation College computing a master's degree in mathematics, more than ten years of corporate informationization work experience. Current research interest, J2EE enterprise application, ERP software research and development, data warehouse system research and development. This article gives a general step in developing the Hibernate Tomcat web application with JBuilder.
1. Download Hibernate-2.1.x.ziphttp: //prdownloads.sourceforge.net/hibernate/? Sort_by = Date & Sort = DESC
2. Installation, extract the zip file to jbuilder_home / thirdparty
3. In the new Hibernate library in JBuilder 2005, select Menu Tools-Configure-Libraries, click the New button in the pop-up dialog box, enter the name of the Hibernate library: Hibernate, click the Add button to add the file hibernate2.jar in the hibernate directory. In the Hibernate library, the corresponding * .jar in the hibernate / lib directory is added to the Hibernate library as needed, which generally requires these categories: DOM4J, CGLIB, COMMONS Collections, Commons Logging, ODMG4, EHCACHE
4. New project files, select the menu file-new project, name to myProject
Set the properties of the project file, select the menu Project-Project Properties, select Tomcat as the server, join the Hibernate library to Path / Required Libraries.
5. New web module, select the menu file-new, named QuickStart for the web module
6. Use the servlet wizard to create a servlet file, select the menu file-new, select the web-standard servlet in the pop-up dialog, name servlet1, in the fifth step of the wizard, select Create a Runtime Configuration
7. Run servlet1
8. Add the file server8080.xml in the QuickStart / Tomcat / Conf directory to the project file, modify the content of Server8080.xml
XML Version = "1.0" encoding = "UTF-8"?>
parameter>
JDBC: Microsoft: SQLServer: // NT04: 1433; DatabaseName = Test value-->
parameter>
com.microsoft.jdbc.sqlserver.sqlserverdriver value ->
parameter>
parameter>
parameter>
parameter>
parameter>
Resourceparams>
Context>
Host>
Engine>
Service>
Server>
9. Set Hibernate, create new files in the QuickStart / src directory, hibernate.cfg.xml as follows:
XML Version = '1.0' encoding = 'UTF-8'?>
PUBLIC "- // Hibernate / Hibernate Configuration DTD // EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
session-factory>
Hibernate-Configuration>
10. Newly built a lasting class
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;
}
}
11. Create a new image in the QuickStart / SRC directory Cat.hbm.xml as follows:
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-maping>
12. Prepare the database
13. Create a new class HibernateUtil
Import net.sf.hibernate. *;
Import net.sf.hibs. *;
Public class hibernateutil {
Private static log log = logfactory.getlog (HibernateUtil.class);
PRIVATE STATIC FINAL SESSIONFACTORY SESSIONFACTORY;
STATIC {
Try {
// Create the sessionFactory
SESSIONFACTORY = 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 currentsession () throws hibernateException {session s = (session) session.get ();
// Open a new session, if this thread HAS NONE YET
IF (s == NULL) {
S = sessionFactory.openSession ();
Session.set (s);
}
Return S;
}
Public static void closesession () THROWS HibernateException {
Session s = (session) session.get ();
session.set (null);
IF (s! = null)
s.close ();
}
}
14. Modify servlet1.java
Package net.sf.hibernate.examples.quickstart;
Import javax.servlet. *;
Import javax.servlet.http. *;
Import java.io. *;
Import java.util. *;
Import net.sf.hibernate.transaction;
Import net.sf.hibernate.hibernateException;
Import net.sf.hibernate.Session;
Import net.sf.hibernate.query;
Public class servlet1 extends httpservlet {
Private static final string content_type = "text / html; charSet = BIG5";
Private hibernateutil HibernateUtil = NULL;
// Initialize Global Variables
Public void init () throws servletexception {
}
// Process the http get request
Public void doget (httpservletRequest Request, HttpservletResponse Response) THROWS
ServletException, IOException {
Response.setContentType (Content_Type);
PrintWriter out = response.getwriter ();
HibernateUtil = new hibernateutil ();
Try {
Session session = hibernateutil.currents;
Transaction tx = session.begintransaction ();
Cat princess = new cat ();
Princess.setname ("Princess");
Princess.setSex ('f');
Princess.setWeight (7.4f);
Session.save (Princess);
TX.comMit ();
Query Query = session.createQuery
"SELECT C from Cat as C where C.sex =: SEX");
Query.setCharacter ("SEX", 'F');
Out.println ("");
Out.println ("
Iteerator it = query.Itemate (); it.hasnext ();) {
Cat Cat = (CAT) IT.Next ();
Out.println ("
female cat:" Cat.GetName () " p>");
}
Out.println (" Body>");
Out.println (" html>");
Out.close ();
TX.comMit ();
Hibernateutil.closesis ();
} catch (hibernateException e) {
E.PrintStackTrace ();
}
}
// Process the http post request
Public Void Dopost (httpservletRequest Request, HttpservletResponse Response) THROWS
ServletException, IOException {
DOGET (Request, Response);
}
// Process the http put request
Public void doput (httpservletRequest Request, httpservletresponse response) throws
ServletException, IOException {
}
// Clean Up Resources
Public void destroy () {
}
}
15. Run Servlet1 again, an error.
16. Set the properties of the project file, select the menu Project-Project Properties, select Build-Resource-XML in the pop-up dialog box, select the selection button Copy. This step is to copy the XML file in the src directory to the classs directory when rebuilding the project file.
17. Run Servlet1 again, you will see the display results after using Hibernate.
"Hibernate.cfg.xml not found".
When testing with JUnit, you need to add as follows:
Reference resource http://www.hibs/reference/en/html/quickstart.html