In fact, Hibernate itself is an independent framework, which does not require any web server or application server support. However, most of the hibernate beginners have added many non-hibernate things, such as Tomcat, Eclipse, log4j, struts, XDoclet, and even JBoss. This can easily lead to a complicated and difficult misunderstanding of hibernate, especially cracking down the enthusiasm of beginners.
This article does not cover eclipse, log4j, struts, tomcat, XDoclet, and JBoss. The purpose of this article is to demonstrate the installation process and basic functions of hibernate, so as to give beginners a lower entry threshold.
<This example is only applicable to hibernate 2.0. If hibernate 3.0 is used, you need to modify the namespace.>
Download files
You need Java SDK, Hibernate package, ant package, and JDBC driver.
1. hibernate package:
Version 2.0: http://nchc.dl.sourceforge.net/sourceforge/hibernate/hibernate-2.1.8.zip
3.3.1 version: http://nchc.dl.sourceforge.net/sourceforge/hibernate/hibernate-distribution-3.3.1.GA-dist.zip
2. Ant package:
Http://labs.xiaonei.com/apache-mirror/ant/binaries/apache-ant-1.7.1-bin.zip
3. JDBC driver depends on the database you use. Generally, it is available on the official database website. Hibernate supports common databases such as MySQL, Oracle, PostgreSQL, and MS-SQL server. These databases all have JDBC driver:
Oracle JDBC Driver (Oracle agreement must be agreed before download)
Http://otn.oracle.com/software/htdocs/distlic.html? /Software/Tech/Java/sqlj_jdbc/htdocs/jdbc9201.html
MySQL JDBC driver
Http://dev.mysql.com/downloads/connector/j/3.0.html
PostgreSQL JDBC driver
Http://jdbc.postgresql.org/download.html
MS-SQL server JDBC driver
Http://www.microsoft.com/downloads/details.aspx? Familyid = 9f1874b6-f8e1-4bd6-947c-0fc5bf05bf71 & displaylang = en
4. decompress the hibernate package and ant package to C:/dev/respectively (this directory is not important. You can change it to any other directory ).
Configure the environment
1. You need to add a new environment variable ant_home to point it to the directory where C:/dev/<your ant package is located>. Add % ant_home %/bin to the path environment variable.
2. You need to add a new environment variable java_home to point it to your j2sdk root directory. Add % java_home %/bin to the path environment variable.
3. Create a project directory, such as C:/workspace/my1sthibernate.
In the project directory, create three other directories: SRC, classes, and Lib.
In the lib directory, create two directories: Hibernate and DB.
In this way, you have the following file structure:
C:/workspace/my1sthibernate/
C:/workspace/my1sthibernate/src
C:/workspace/my1sthibernate/classes
C:/workspace/my1sthibernate/lib
C:/workspace/my1sthibernate/lib/hibernate
C:/workspace/my1sthibernate/lib/DB
4. Copy the
Copy all files under
Copy your JDBC driver file (usually a jar file) to C:/workspace/my1sthibernate/lib/DB.
Create a database
1. Use your favorite database software to create a database of hibernate_test. <I am using MySQL 5.0>
2. In this database, create a new table named customer
Create Table customer
(
CID integer not null primary key,
Username varchar (12) not null,
Password varchar (12)
);
Compile a Java File
Public Class Customer {</P> <p> private int ID; <br/> private string username; <br/> private string password; </P> <p> Public int GETID () {<br/> return ID; <br/>}</P> <p> Public String GetPassword () {<br/> return password; <br/>}</P> <p> Public String GetUserName () {<br/> return username; <br/>}</P> <p> Public void setid (int id) {<br/> This. id = ID; <br/>}</P> <p> Public void setpassword (string password) {<br/> This. password = password; <br/>}</P> <p> Public void setusername (string username) {<br/> This. username = username; <br/>}</P> <p >}< br/>
Save this class as the C:/workspace/my1sthibernate/src/customer. Java file.
Compile the test class
Import net. SF. hibernate. *; <br/> Import net. SF. hibernate. cfg. *; <br/> // if you are using version 3.0, You need to modify the package name introduction. <br/> // import Org. hibernate. *; <br/> // import Org. hibernate. cfg. *; </P> <p> public class test {</P> <p> Public static void main (string [] ARGs) {</P> <p> try {<br/> sessionfactory Sf = <br/> new configuration (). configure (). buildsessionfactory (); <br/> session = SF. opensession (); <br/> transaction Tx = session. begintransaction (); </P> <p> for (INT I = 0; I <200; I ++) {<br/> customer = new customer (); <br/> customer. setusername ("customer" + I); <br/> customer. setpassword ("customer"); <br/> session. save (customer); <br/>}</P> <p> Tx. commit (); <br/> session. close (); </P> <p >}catch (exception e) {<br/> // E. printstacktrace (); <br/>}< br/>
Save this class as the C:/workspace/my1sthibernate/src/test. Java file.
Create a hibernate ing File
Because there is only one class --- customer and one table --- customer, you only need to create a customer ing file --- customer. HBM. XML to correspond to the relationship between the customer class and the customer table.
<? XML version = "1.0"?> <Br/> <! Doctype hibernate-mapping Public <br/> "-// hibernate/hibernate DTD ing DTD/EN" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> </P> <p> <pibernate- mapping> <br/> <class name = "customer" table = "customer"> <br/> <ID name = "ID" column = "CID"> <br/> <generator class = "increment"/> <br/> </ID> <br/> <property name = "username" column = "username"/> <br/> <property name = "password" column = "password"/> <br/> </class> <br/> </pibernate-mapping> <br/>
Save this file as C:/workspace/my1sthibernate/src/customer. HBM. xml and put it in the same directory as customer. java.
Compile the ant build. xml file
You do not have to know the details of this build. xml. In fact, ant is not required by hibernate. Here ant is used to simplify some tasks, such as compiling, copy, running, and so on. This file is stored in C:/workspace/my1sthibernate.
<? XML version = "1.0"?> </P> <p> <project name = "my1sthibernate" default = "build" basedir = "."> <br/> <! -- If compilation fails, you can change default = "build" to default = "run" --> </P> <p> <property name = "base. dir "value = ". "/> <br/> <property name =" src. dir "value =" src "/> <br/> <property name =" Lib. dir "value =" lib "/> <br/> <property name =" build. dir "value =" classes "/> </P> <p> <path id =" myclasspath "> <br/> <fileset dir =" $ {Lib. dir} "> <br/> <include name = "**/*. jar "/> <br/> </fileset> <br/> <pathelement location =" $ {build. dir} "/> <Br/> <! -- If the statement cannot be compiled, add this sentence <br/> <pathelement location = ". "/> <br/> --> <br/> </path> </P> <p> <target name =" init "> <br/> <mkdir dir = "$ {build. dir} "/> <br/> </Target> </P> <p> <target name =" build "depends =" init "Description =" compile the source files"> <br/> <javac classpathref = "myclasspath" srcdir = "$ {SRC. dir} "destdir =" $ {build. dir} "/> <br/> <copy todir =" $ {build. dir} "> <br/> <fileset dir =" $ {SRC. dir} "> <br/> <exclude name = "**/*. java "/> <br/> </fileset> <br/> </copy> <br/> </Target> </P> <p> <target name =" run "depends =" build "> <br/> <Java classpathref =" myclasspath "classname =" test "fork =" true "/> <br/> </Target> </P> <p> <target name = "clean"> <br/> <Delete multiple deemptydirs = "true"> <br/> <fileset dir = "$ {build. dir} "/> <br/> </delete> <br/> </Target> </P> <p> </Project> <br/>
Configure the hibernate description file
The Hibernate description file can be a properties or XML file, the most important of which is to define the database connection. Here is a hibernate. cfg. XML description file in XML format.
For a MySQL database, the content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype hibernate-configuration public "-// hibernate/hibernate configuration DTD/EN" <br/> "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> </P> <p> <pibernate-configuration> </ p> <p> <session-factory> <br/> <property name = "hibernate. connection. driver_class "> <br/> COM. mySQL. JDBC. driver </property> <br/> <property name = "hibernate. connection. URL "> JDBC: mysql: // 127.0.0.1: 3306/hibernate_test </Property> <br/> <property name = "hibernate. connection. username "> root </property> <br/> <property name =" hibernate. connection. password "> 123 </property> <br/> <property name =" hibernate. connection. pool. size "> 20 </property> <br/> <property name =" hibernate. show_ SQL "> true </property> <br/> <property name =" JDBC. fetch_size "> 50 </property> <br/> <property name =" JDBC. batch_size "> 25 </property> <br/> <property name =" JDBC. Use_scrollable_resultset "> false </property> <br/> <property name =" hibernate. dialect "> net. SF. hibernate. dialect. mysqldialect </property> </P> <p> <! -- Mapping Files --> <br/> <Mapping Resource = "customer. HBM. XML "/> <br/> </session-factory> <br/> </pibernate-configuration>
For Oracle databases
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype hibernate-configuration <br/> Public "-// hibernate/hibernate configuration DTD/EN" <br/> "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> </P> <p> <pibernate- configuration> </P> <p> <session-factory name = "Java: /hibernate/hibernatefactory "> </P> <p> <property name =" show_ SQL "> true </property> <br/> <property name =" connection. driver_class "> <br/> oracle. JDBC. driver. oracledriver <! -- Here is the JDBC Driver Class Name of Oracle 9i --> <br/> </property> <br/> <property name = "connection. URL "> <br/> JDBC: oracle: oci8: @ hibernate_test <! -- Here is the URL of the Oracle hibernate_test database --> <br/> </property> <br/> <property name = "connection. username "> <br/> your database username <br/> </property> <br/> <property name =" connection. password "> <br/> Your Database Password <br/> </property> <br/> <property name =" dialect "> <br/> net. SF. hibernate. dialect. oracle9dialect <! -- Here is the dialect of Oracle 9i --> <br/> </property> </P> <p> <Mapping Resource = "customer. HBM. xml"/> <! -- Specify the customer's customer ing file --> </P> <p> </session-factory> </P> <p> </pibernate-configuration> <br/>
If you are not using Oracle 9i or MySQL, go to C:/dev/<directory of your hibernate package>/src/hibernate. find your database in the properties file, and then replace the above values.
Start running
Run ant run in C:/workspace/my1sthibernate. If you strictly follow the above steps, you should see
Run:
[Java] log4j: warn no appenders cocould be found for logger (net. SF. hibernate. cfg. Environment ).
[Java] log4j: Warn please initialize the log4j system properly.
[Java] hibernate: insert into customer (username, password, CID) values (?, ?, ?)
Build successful
Go to your database hibernate_test and check that 200 new records are added to the custmor table, but you have not written any JDBC code.
If you want to change the database in the future, you only need to change the value in the hibernate. cfg. XML description file.
Conclusion
This article is a very low entry level introduction. It takes less than 30 minutes for you to run your first hibernate program, which will arouse your interest in hibernate. But readers must realize that this is only the beginning. This article is a glimpse of a small ice crystal on the tip of the hibernate iceberg. A journey of a thousand miles begins with a single step. You can use this article as a starting point for the hibernate journey.