Hibernate Framework Learning: Hibernate Framework Overview

Source: Internet
Author: User
Scene Reproduction

Suppose we need to develop a student information management system, then we will inevitably need to write and database interaction code, which is what we often say the data

Persistence layer, before which we have written a lot of persistence layer code, namely JavaBean and DAO.

The following is the design of a student entity class:

Package com.demo.domain;

Import Java.util.Date;
	/** * Student Entity class * @author Administrator * @date December 3, 2016 * * public class Student {private int sid;
	Private String sname;
	Private String gender;
	Private Date birthday;
	
	Private String address; Public Student () {} public Student (int sid, String sname, String gender, Date birthday, string address) {This.si
		d = SID;
		This.sname = sname;
		This.gender = gender;
		This.birthday = Birthday;
	this.address = address;
	} public int GetSID () {return SID;
	} public void Setsid (int sid) {this.sid = SID;
	} public String Getsname () {return sname;
	} public void Setsname (String sname) {this.sname = sname;
	} public String Getgender () {return gender;
	} public void Setgender (String gender) {This.gender = gender;
	Public Date Getbirthday () {return birthday;
	} public void Setbirthday (Date birthday) {this.birthday = birthday;
	} public String getaddress () {return address;
}	
	public void setaddress (String address) {this.address = address; } @Override Public String toString () {return "Student [sid=" + Sid + ", sname=" + sname + ", gender=" + Gender + ",
	Birthday= "+ Birthday +", address= "+ address +"] "; }

}

It is necessary to establish a database in a relational database and create a corresponding data table, that is, to generate one such data table student:

Then we generate a lot of student objects based on this entity class, and a table in the relational database, so this process requires us to write large

Volume of SQL statements.

What's wrong with writing SQL statements?

Different databases use different SQL syntax. For example: PL/SQL and t/sql;

The same functionality has different implementations in different databases. such as paging SQL;

The program relies too much on SQL to transplant and extend the program, maintenance, etc. bring great trouble;

So what do we have to give up the process of writing a lot of SQL statements, using object-oriented thinking to develop the data persistence layer. So

The hibernate framework is an ORM framework.


The origin of the hibernate framework

If you have learned javaweb, then for the classic three-layer architecture will not be unfamiliar, three-tier architecture of the three-layer refers to the interface layer, business logic

layer, data persistence layer. In Javaweb, the interface layer and the business logic layer can use the struts framework to encapsulate some common operations. And in our number

According to the persistent layer, the database additions and deletions, we often have to manually control transactions, various SQL query queries, all kinds of parameters passed, regardless of which

Table operations are used in the same way of thinking to operate. Since it is the same way of thinking, that is the repetition of things. And we're going to have a relational database.

Dealing, not just the object. And the Hibernate framework is a perfect package for this, so we can use Hibernate to make us adopt

object's mind-manipulating relational database, which is consistent with our thinking form.


Open Source ORM Framework

Before learning the hibernate framework, you need to understand the ORM framework first.

The ORM's full name is ORM (Object/relationship Mapping): Object/Relationship mapping. The application of database based on object-oriented thought

The final sequence is to keep the object information in the relational database, so write many SQL statements related to the underlying database. The idea and the object-oriented

are very different.

ORM is a technique to solve the phenomenon of non-matching between object-oriented and relational database. Simply put, ORM is used to describe the

The metadata that is mapped between the image and the database, automatically persisting objects in the Java program to the relational database. is essentially converting data from one form to another

to a different form.

The object-oriented development method is the mainstream development method in the enterprise application development environment, and the relational database is the permanent storage number in enterprise-level application environment.

The main data storage system. The letter O originates from "object", and R is derived from "relationship" (relational). In almost every program.

Both objects and relational databases exist. In the business logic layer and the rendering layer, we are object-oriented. When the object information is changed, we need to

To save the object's information in a relational database.

When you develop an application (without using ORM), you may write a lot of data access layer code to save, delete, read from the database

Object information, and so on. And the code is always repeating itself.

Save the data to a database or some storage device. In a three-tier architecture, persistence is the level of dealing with the database. Web Development in JSP

, often there are many database connection, delete, query and other operations, in the database related work through JDBC too cumbersome, it spawned an ORM framework,

An ORM is an automatic mapping between a relational database and an object, so that you do not need to use complex SQL statements to manipulate the database, as long as the operation

The ORM tool automatically converts the object to an SQL statement operation. This only requires attention to the structure of the object in the business logic, without concern for the underlying complex

Mixed SQL and JDBC code. The hibernate framework is one of the ORM frameworks and is a solution for relational/object mapping.

ORM Framework:

Other mainstream ORM Framework technologies:

MyBatis: Formerly known as Ibatis

Toplink: Later acquired by Oracle and repackaged as Oracle as Toplink

EJB: itself a specification for Java EE (Heavyweight framework)


Hibernate Framework Introduction

The hibernate framework is a Java-based object/relational database mapping tool that maps the data represented by the object model to the relational modules represented by SQL

Type up. Hibernate manages Java-to-database mapping, provides methods for querying and accessing data, and reduces programmer-related data persistence layers

Programming tasks.

The hibernate framework is an object/relational mapping framework that is designed to map objects represented by the object model to relational database structures.

The hibernate framework is an open-source ORM Framework technology in the Java world. The hibernate framework makes a very lightweight object encapsulation of JDBC.

Object/Relationship mapping has always been a difficult point in database technology, although many solutions have been proposed to solve this problem, but they can not be fully made to facilitate

and efficient. The introduction of EJBS has seen hope, but the practice proves that entity beans are inefficient and difficult for beginners to understand. By

The hibernate framework, created by Gavin King, is moving in the right direction from some program and is recognised by more and more it practitioners.

Like the struts framework of the year, Hibernate has been widely used in many projects. Hibernate as a result of betting more energy in the promotion

Efficiency, the use is very convenient, the new version of the EJB specification is moving toward hibernate direction. Thanks to its extensive recognition, Hibernate has

Become one of the techniques that programmers must master.

The role hibernate plays in the application:


Hibernate high-level architecture diagram:


Hibernate detailed architecture diagram:

Hibernate Framework Roles:

Hibernate core interfaces and classes

There are 6 core classes and interfaces for Hibernate, namely: Session, Sessionfactory, Transaction, Query, criteria, and

Configuration. These 6 core classes and interfaces will be used in any development. Through these interfaces, you can not only access persistent objects, but also

Sufficient for transaction control. These 6 core interfaces and classes are described separately below.

Session

The session interface is responsible for performing crud operations on persisted objects (the task of crud is to complete the communication with the database, including many common SQL language

Sentence). However, it is important to note that the session object is non-thread-safe. At the same time, Hibernate's session differs from the HttpSession in JSP applications. This

When using the term session, in fact refers to hibernate in the session, and later will be HttpSession object called the user session.

For the session we are so familiar with, in the Javaweb development session can be used to save the user session state information. and

The session in Hibernate is the object-to-database session Persistence Manager.

A session is a single-threaded, short-lived object that represents a conversation between an application and a persistence layer, encapsulating the engineering of a JDBC connection, a transaction. Save Required

The object cache needs to be persisted, used to traverse the object graph, or to represent a Lookup object.

Sessionfactory

The Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The work is used here.

Factory mode. Note that sessionfactory is not lightweight, because typically a project requires only one sessionfactory

Enough, you can specify a sessionfactory for each database when you need to manipulate multiple databases.

A session factory is a thread-safe, immutable cache snapshot of a mapped file that belongs to a single database. It is a session of the factory class that may hold a

An optional (level two) database cache that can hold data that can be reused in a transaction at the process level or at the cluster level.

Transaction

Transaction interface is an optional API, you can choose not to use this interface, instead of the hibernate designers write their own bottom

The transaction-processing code. The Transaction interface is an abstraction of the actual transaction implementation, including the JDBC transaction, the JTA

UserTransaction, or even a CORBA transaction. This design is designed to enable developers to use a unified transaction interface, so that

Their projects can be easily ported between different environments and containers.

Single thread, which the application uses to represent a batch of indivisible operations. A session may span multiple transactions in some cases.

Query

The query interface makes it easy to make queries to databases and persistent objects, which can be expressed in two ways: the HQL language or the local database's SQL

Sentence Queries are often used to bind query parameters, limit the number of query records, and ultimately perform query operations.

Criteria

The criteria interface is very similar to the query interface, allowing you to create and execute object-oriented, standardized queries. It is important to note that the criteria interface is also light

Magnitude, it cannot be used outside the session.

Configuration

The purpose of the configuration class is to configure Hibernate and start it. During Hibernate's start-up process,

An instance of the configuration class first locates the location of the mapped document, reads the configuration, and then creates a Sessionfactory object. Although

The Configuration class plays only a small role in the entire Hibernate project, but it is the first one that is encountered when starting hibernate

Like. Hibernate pros and cons

Advantages:

Because hibernate is used, no specific JDBC statements are involved in the code, so the portability of the code is facilitated;

We use the Java Development Program, then using the object-oriented thinking, using Hibernate allows us to use the object-based thinking of manipulating relational data

Library, hibernate two different models to establish the association between, Hibernate provides us with the use of object-oriented thinking to manipulate the relational data of the connection

The development of the object (impedance mismatch);

The hibernate framework is a lightweight framework that is non-intrusive, non-intrusive, and supports transparent persistence;

The steps to working with the database directly using JDBC are cumbersome, and using the Hibernate framework is efficient and can improve productivity.

Disadvantages:

Encapsulation is too thorough to lead to inflexible, is to use data-specific things relatively not easy;

There is a problem with updating a large number of databases, when a large number of query statistics, or batch update can not use the database features mechanisms, such as stored procedures;

Statements that use database attributes will be difficult to tune;





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.