Skip to main content

Development

Basic usage about Hibernate

What is Hibernate?

Hibernate, as an ORM (Object rational Mapping) solution, effectively “sits between” the Java application data access layer and the Relational Database, as can be seen in the diagram above. The Java application makes use of the Hibernate APIs to load, store, query, etc its domain data.

Why use Hibernate?

High Cross-database portability

It is completely compatible with all databases supported by JDBC driver, like MySQL, SQL Server, Oracle. And provides convenient transition among different databases with configuration of Dialect, no else actions are needed.

High performance

Provides first-level and second-level cache system to improve database-related operation efficiency, reducing actual interaction with low-level databases, improving system performance.

High developer’s productivity

Although it takes time for a developer to learn Hibernate in the beginning, however, after you master it, complicated SQL sentences are not needed any more in your code. Just a few lines of codes could complete all database-related operations, addition, deletion, update, query. More clean, simple codes are used inside Hibernate, achieving higher productivity.

4 important classes defined inside Hibernate

Configuration class

Configuration class is mainly responsible to manage Hibernate configuration information, launch Hibernate, load low-level data like Database URL, DB User name, DB password, DB driver and DB Dialect, etc during Hibernate running time.

Session Factory

A thread-safe (and immutable) representation of the mapping of the application domain model to a database. Acts as a factory for org.hibernate.Session instances. The EntityManagerFactory is the JPA equivalent of a SessionFactory and basically those two converge into the same SessionFactory implementation.

A SessionFactory is very expensive to create, so, for any given database, the application should have only one associated SessionFactory. The SessionFactory maintains services that Hibernate uses across all Session(s) such as second level caches, connection pools, transaction system integrations, etc.

Session

A single-threaded, short-lived object conceptually modeling a “Unit of Work” PoEAA. In JPA nomenclature, the Session is represented by an EntityManager.

Behind the scenes, the Hibernate Session wraps a JDBC java.sql.Connection and acts as a factory for org.hibernate.Transaction instances. It maintains a generally “repeatable read” persistence context (first level cache) of the application domain model.

Transaction

A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries. EntityTransaction is the JPA equivalent and both act as an abstraction API to isolate the application from the underlying transaction system in use (JDBC or JTA).

Installation

Download URL

http://www.hibernate.org latest version is hibernate-release-5.2.10.Final.

Unzip package and copy jar files under hibernate-release-5.2.10.Final\lib\required to project lib directory. If it is a web project, these jar files need to be copied to WebContent\WEB-INF\lib directory as well.

Guide Documents

1) quick start

hibernate-release-5.2.10.Final\documentation\quickstart\html_single\index.html

2) User guide

hibernate-release-5.2.10.Final\documentation\userguide\html_single\Hibernate_User_Guide.html

System requirements

Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.

Create MySQL Tables

hibernate.cfg.xml

Create Book Class

Register Hibernate Mapping

Create Hibernate Session Factory

Add a record

Delete a record

Update a record

Query all records

Hibernate native query language

Criteria query mode

Native SQL query mode

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram