In fact, hibernate itself is an independent framework that does not require any support from Web server or application server. However, most of the hibernate introductory introductions include a very large number of non-hibernate things, such as Tomcat, Eclipse, Log4j,struts, XDoclet, and even JBoss. This easy makes it difficult for people to understand the misunderstanding of hibernate, especially the enthusiasm of the people who have just started learning.
In this article will not involve Eclipse, log4j, Struts, Tomcat, XDoclet, and JBoss. The purpose of this article is to demonstrate Hibernate's installation process and the most important features to give the person who just started learning a lower entry threshold.
Download File
You need the Java SDK, hibernate package, ant package, and JDBC Driver.
1. Hibernate package:
Http://prdownloads.sourceforge.net/hibernate/?sort_by=date&sort=desc
2. Ant Pack:
Http://apache.130th.net/ant/binaries/apache-ant-1.6.1-bin.zip
3, JDBC driver to be based on the database you use to set, the general database on the official site will have. Hibernate supports frequently used database, such as MySQL, Oracle, PostgreSQL, and Ms-sql Server. These databases all have JDBC Driver:
Oracle JDBC Driver (Oracle protocol must be allowed before downloading)
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. Extract the hibernate package and ant package separately to c:/dev/(this folder is not important, you can change the other folders regardless of the folder).
Configuring the Environment
1. You need to join? A new environment variable: ant_home, make it point to c:/dev/< your ANT package is in the folder >. and add the%ant_home%/bin to the PATH environment variable.
2. Do you need to join? A new environment variable: java_home, let it point to your J2SDK root folder. and add the%java_home%/bin to the PATH environment variable.
3. Create a project folder, for example C:/workspace/my1sthibernate.
Under the project folder, create another three folders: SRC, classes, lib.
Under the Lib folder, create two folders: Hibernate and DB.
So you have a file structure such as the following:
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, will c:/dev/< your Hibernate package folder >/hibernate2.jar file copy to C:/workspace/my1sthibernate/lib/hibernate.
Copy all the files under the folder >/lib/c:/dev/< your hibernate package to c:/workspace/my1sthibernate/lib/hibernate.
Copy your JDBC driver file (usually a jar file) to c:/workspace/my1sthibernate/lib/db.
Create a database
1, use your favorite database software, create a hibernate_test.
2. Under this database, create a new table named Customer
CREATE TABLE CUSTOMER
(
CID INTEGER not NULL PRIMARY KEY,
USERNAME VARCHAR () not NULL,
PASSWORD VARCHAR (12)
);
Writing Java files
public class Customer {
private int id;
Private String username;
private String password;
public int getId () {
return ID;
}
Public String GetPassword () {
return password;
}
Public String GetUserName () {
return username;
}
public void setId (int id) {
This.id = ID;
}
public void SetPassword (String password) {
This.password = password;
}
public void Setusername (String username) {
This.username = Username;
}
}
Save this class as a C:/workspace/my1sthibernate/src/customer.java file.
Writing the test class
Import net.sf.hibernate.*;
Import net.sf.hibernate.cfg.*;
public class Test {
public static void Main (string[] args) {
try {
Sessionfactory SF =
New Configuration (). Configure (). Buildsessionfactory ();
Session session = Sf.opensession ();
Transaction tx = Session.begintransaction ();
for (int i = 0; i <; i++) {
Customer customer = new Customer ();
Customer.setusername ("customer" + i);
Customer.setpassword ("Customer");
Session.save (customer);
}
Tx.commit ();
Session.close ();
} catch (Hibernateexception e) {
E.printstacktrace ();
}
}
}
Save this class as a C:/workspace/my1sthibernate/src/test.java file.
creating hibernate mapping Files
Since there is only one class---customer and one table---customer, you simply need to create a mapping file---Customer.hbm.xml to correspond to the relationship between the customer class and the Customer table.
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping dtd//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<class name= "Customer" table= "Customer" >
<id name= "id" column= "CID" >
<generator class= "Increment"/>
</id>
<property name= "username" column= "username"/>
<property name= "password" column= "password"/>
</class>
Save this file as C:/workspace/my1sthibernate/src/customer.hbm.xml, and Customer.java in the same folder.
Writing ant build.xml files
You don't have to know the details of this build.xml, in fact ant is not a must for hibernate. Ant is used here to simplify some tasks, such as compiling, copy, executing, and so on.
<?xml version= "1.0"?>
<project name= "my1sthibernate" default= "Build" basedir= "." >
<property name= "Base.dir" value= "."/>
<property name= "Src.dir" value= "src"/>
<property name= "Lib.dir" value= "Lib"/>
<property name= "Build.dir" value= "Classes"/>
<path id= "Myclasspath" >
<fileset dir= "${lib.dir}" >
<include name= "**/*.jar"/>
</fileset>
<pathelement location= "${build.dir}"/>
</path>
<target name= "Init" >
<mkdir dir= "${build.dir}"/>
</target>
<target name= "Build" depends= "Init" description= "compile the source files" >
<javac classpathref= "Myclasspath" srcdir= "${src.dir}" destdir= "${build.dir}"/>
<copy todir= "${build.dir}" >
<fileset dir= "${src.dir}" >
<exclude name= "**/*.java"/>
</fileset>
</copy>
</target>
<target name= "Run" depends= "Build" >
<java classpathref= "Myclasspath" classname= "Test" fork= "true"/>
</target>
<target name= "clean" >
<delete includeemptydirs= "true" >
<fileset dir= "${build.dir}"/>
</delete>
</target>
</project>
Configuring Hibernate descriptive narrative files
Hibernate descriptive narrative files can be a properties or XML file, the most important of which is to define the connection of the database. What I'm listing here is an XML-formatted hibernate.cfg.xml descriptive narrative file.
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE hibernate-configuration
Public "-//hibernate/hibernate Configuration dtd//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd" >
<session-factory name= "Java:/hibernate/hibernatefactory" >
<property name= "Show_sql" >true</property>
<property name= "Connection.driver_class" >
Oracle.jdbc.driver.OracleDriver <!--This is the Oracle 9i JDBC Driver class name--
</property>
<property name= "Connection.url" >
Jdbc:oracle:oci8: @hibernate_test <!--here is Oracle's hibernate_test database URL--
</property>
<property name= "Connection.username" >
Your database user name
</property>
<property name= "Connection.password" >
Your database Password
</property>
<property name= "dialect" >
Net.sf.hibernate.dialect.Oracle9Dialect <!--here is dialect of Oracle 9i
</property>
<mapping resource= "Customer.hbm.xml"/> <!--Specify a mapping file for Customer--
</session-factory>
Assuming you're not using Oracle 9i, you can find your database in the folder >/src/hibernate.properties file where you c:/dev/< your hibernate package, and replace the corresponding values above.
Start Execution
Under C:/workspace/my1sthibernate, execute ant run. Assuming you strictly follow the above steps, you should see
Run
[Java] Log4j:warn No Appenders could is found for logger (net.sf.hibernate.cfg.Environment).
[Java] Log4j:warn initialize the log4j system properly.
[Java] Hibernate:insert into CUSTOMER (USERNAME, PASSWORD, CID) VALUES (?,?,?)
BUILD Successful
Take a look at your Hibernate_test database and add 200 new records to the Custmor table, but you didn't write any JDBC code.
After assuming you want to change the database, only need to change the hibernate.cfg.xml descriptive narrative file corresponding value can be.
Conclusion
This article is a very low threshold introduction. I gave a friend who knew nothing about hibernate, and he took less than 30 minutes to execute his first hibernate program, which aroused his interest in hibernate. But the reader must realize that this is only the beginning of a small ice crystal in the corner of the iceberg. You can take this as a starting point towards the Hibernate boulevard.
Simple introduction to the simplest Hibernate entry in history