ORM Advanced Hibernate Introduction and Framing

Source: Internet
Author: User

The previous blog explained the ORM idea, as well as the pros and cons of Orm, and said that ORM has a lot of implementation, we will be based on hibernate to explain.

Hibernate is a lightweight, metadata-based ORM framework:

1, metadata (meta data):d ATA about data (data), that is, describing an object's data, equivalent to the context of the object.

2, Lightweight: less resource-intensive, no intrusion. (In fact, I think this is relatively, if compared with ibatis, it into a heavyweight).

Hibernate is a database-oriented program, which allows us to put more effort into the business logic of the project, rather than the database operation, by making the database programming into object-oriented programming.


This blog for everyone to talk about how to build a hibernate framework from scratch!

1. Preparation

A) Java development tools, Eclipse, MyEclipse, Netbean This article takes MyEclipse as an example!

b) jar package, hibernate required jar, connect MySQL required jar

Antlr-2.7.6.jar (Generate SQL statement)

Asm.jar (Byte Code enhancement Tool Class)

C3p0-0.9.1.jar (data source connection pool component)

Cglib-2.1.3.jar (proxy component, inheritance-based)

Commons-collections-2.1.1.jar (Collection tool class components for efficient operation)

Commons-logging-1.0.4.jar (conversion component for log output)

Log4j-1.2.11.jar (log output component, more detailed and can control output format, and destination)

Dom4j-1.6.1.jar (XML parsing)

Ehcache-1.2.3.jar (Cache component)

Ejb3-persistence.jar (Specification jar package for persistent operation)

Hibernate3.jar (Core jar package for the framework)

Jta.jar (Global transaction Management)

Junit-3.8.1.jar (unit test)

Mysql-connector-java-3.1.13-bin.jar (MySQL database driver jar pack)

c) MySQL database one!

2. Build

A) first create a new Java project named Hibernate here

b) Right-click on the Hibernate project to select Build Path-àconfigure build Path

c) The following interface appears, select Add JARs, add the required jar package. Then OK.

D) Set up two packages under SRC, Com.tgb.domain and Com.tgb.service, respectively, add a name of Hibernate.cfg.xml, this file is best to download one on the Internet, the province's own write wrong. The directory structure is as shown.

e) Then create the user class and the User.hbm.xml file under Com.tgb.domain, create the client class and the UserService class under Com.tgb.service, as shown!

3. Write Code

a) The user class code is as follows

Package com.tgb.domain;/** * User entity * @author Hongjie * */public class User {//primary key private int id;//username private String Userna me;//Password private String  passwords;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetUserName () {return username;} public void Setusername (String username) {this.username = username;} Public String Getpasswords () {return passwords;} public void Setpasswords (String passwords) {this.passwords = passwords;}}

b) The code in User.hbm.xml is as follows

<?xml version= "1.0"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd "><!--   package-->

c) client|. Java code is as follows

Package Com.tgb.service;public class Client {/** * @param args */public static void main (string[] args) {//Create an instance of a service Userse Rvice userservice = new UserService ();//Call to Add Method Userservice.add ();}}

d) The code for Userservice.java is as follows

Package Com.tgb.service;import Org.hibernate.session;import Org.hibernate.sessionfactory;import Org.hibernate.transaction;import Org.hibernate.cfg.configuration;import Com.tgb.domain.user;public Class UserService {public void  Add () {//Create a   configuration  configuration cfg = new configuration (). Configure ();  //Create Sessionfactory         Sessionfactory factory = Cfg.buildsessionfactory ();                   Session session = Factory.opensession ();  Turn on transaction          System.out.println ("open transaction"); Transaction tran = Session.begintransaction ();  User user = new user ();  User.setusername ("Zhanghongjie"); User.setpasswords ("Zhanghongjie");//save Session.save (user)  ; System.out.println ("Save in Database");//Commit a transaction. Save to Database Tran.commit ();      Session.close ();  }}

e) Hibernate.cfg.xml code is as follows

<! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">

4. Testing

A) Hibernate can automatically create a database table, can not create a database, so we have to create a hibernatetest database ourselves first. The creation process will not say, this is the knowledge of MySQL, will not be able to go to fill up. Databases that are very common.

b) Let's see if there are any tables in the Hibernatetest database!

Right-click on the client and select Run as---Java application.

The console console will then output the following information

We went to the database to query, found that the table has been established, the data inserted into the database.

Here's how our framework is built!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

ORM Advanced Hibernate Introduction and Framing

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.