The simplest hibernate introduction in history

Source: Internet
Author: User

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.

In this articleArticleEclipse, log4j, struts, tomcat, XDoclet, and JBoss will not be involved. 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.

Download files

You need Java SDK, Hibernate package, ant package, and JDBC driver.

1. hibernate package:
Http://prdownloads.sourceforge.net/hibernate? Sort_by = Date & sort = DESC

2. Ant package:
Http://apache.130th.net/ant/binaries/apache-ant-1.6.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 c: \ Dev \ <directory of your hibernate package> \ hibernate2.jar file to C: \ workspace \ my1sthibernate \ Lib \ hibernate.

Copy all files under c: \ Dev \ <directory of your hibernate package> \ Lib \ to C: \ workspace \ my1sthibernate \ Lib \ hibernate.

Copy your JDBC driver file (generally 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.

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 {

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 the C: \ workspace \ my1sthibernate \ SRC \ customer. Java file.

Compile 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 = SF. opensession ();
transaction Tx = session. begintransaction ();

For (INT I = 0; I <200; 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 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"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD // en"
Http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd>

<Hibernate-mapping>
<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>
</Hibernate-mapping>

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.

<? 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 = "run" depends = "build">
<Java classpathref = "myclasspath" classname = "test" fork = "true"/>
</Target>

<Target name = "clean">
<Delete multiple deemptydirs = "true">
<Fileset dir = "$ {build. dir}"/>
</Delete>
</Target>

</Project>

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.

<? 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>

<Hibernate-configuration>

<Session-factory name = "Java:/hibernate/hibernatefactory">

<Property name = "show_ SQL"> true </property>
<Property name = "connection. driver_class">
Oracle. JDBC. Driver. oracledriver <! -- Here is the JDBC Driver Class Name of Oracle 9i -->
</Property>
<Property name = "connection. url">
JDBC: oracle: oci8: @ hibernate_test <! -- Here is the URL of the Oracle hibernate_test database -->
</Property>
<Property name = "connection. username">
Your database username
</Property>
<Property name = "connection. Password">
Your Database Password
</Property>
<Property name = "dialect">
Net. SF. hibernate. dialect. oracle9dialect <! -- Here is the dialect of Oracle 9i -->
</Property>

<Mapping Resource = "customer. HBM. xml"/> <! -- Specify the customer's customer ing file -->

</Session-factory>

</Hibernate-configuration>

If you are not using Oracle 9i, go to c: \ Dev \ <directory where your hibernate package is located> \ 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. I showed it to a friend who does not understand hibernate. He ran his first hibernate in less than 30 minutes.ProgramIt has aroused his 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.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/doodoofish/archive/2004/07/16/43207.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.