DEVELOPING WEB APPLICATIONS APPLYING HIBERNATE
FRAMEWORK
SVTH: Nguyễn Khắc HIếu, Nguyễn Tuấn Anh,
Trần Việt Dung, Lâm Thị Thương Huyền,
Vũ Thành Trung, Lê Thị Lan
GVHD: ThS Đỗ Thùy Dương
Tóm tắt - Ngày nay, một số lượng lớn các trang web đã được tạo ra cho nhiều mục đích. Cùng
với đó là sự phát triển của các framework lập trình web như Express, Laravel, Spring,… Hibernate
framework ra đời là hệ quả tất yếu của sự phát triển chung và chứng tỏ giá trị của nó. Trọng lượng
nhẹ, hiệu suất nhanh và tạo bảng tự động chỉ là một số ưu điểm của nó khiến Hibernate trở thành "lựa
chọn được lựa chọn" cho dự án.
Abstract - Now days, a large number of websites have been created for plenty purposes. Along
with that is the development of web programming framework such as Express, Laravel, Spring, and
etc. Hibernate framework comes an inevitable consequence of general development and proves its
worth. Lightweight, fast performance, and automatic table creation are only some of its advantages
which make Hibernate become “the chosen one” for the project.
Keywords – Hibernate, ORM, Object Mapping.
I. INTRODUCTION
Hibernate also known as Hibernate ORM is a framework for Java programming
language which was developed by Red hat, an American multinational Software company, in
2001. It reduces the complexities from a process of developing a java application to interact
with the database. It is an open source, lightweight, and ORM (Object Relational Mapping)
tool. Hibernate applies JPA (Java Persistence API) for the perseverance of data [1].
There are lots of benefits from Hibernate, such as open source and lightweight, fast
performance, database independent query, scalability, lazy-loading, and the most important
advantage, easy to learn [2] which is the main reason why Hibernate was selected to be
applied in this project.
The project Online Banking Application is sensible to use Hibernate framework
because a banking app often has to handle large amount of data and work much with
database. Besides, in some cases, there is a huge number of users accessing at the same time,
it requires the web app to provide with a fast speed of processing.
II. MAIN CONTENT
A. What is Hibernate?
165
Hibernate is a Java framework that makes it easier to create database-interactive Java
applications. It's an ORM (Object Relational Mapping) tool that's open source and
lightweight. For data persistence, Hibernate implements the JPA (Java Persistence API)
specifications.
Hibernate offers an abstraction layer, which means that the programmer doesn't have to
think about the implementations; Hibernate handles them internally, such as establishing a
database link, writing queries to perform CRUD operations, and etc.
B. Why Hibernate?
As regarded above, Hibernate is one of the most popular Java frameworks that bring a
lot of advantages for developer:
1. Open source and lightweight: Hibernate Framework is an open source with LGPL
license and is lightweight.
2. Fast Performance: The performance of Hibernate Framework is fast because the
cache is used internally by Hibernate Framework. There are two types of cache in Hibernate
Framework, the first level cache and the second level cache. First level cache is enabled by
default.
3. Independent Database Query: HQL (Hibernate Query Language) is the objectoriented version of SQL. It creates independent database queries. So, you don't need to write
database specific queries. Before Hibernate, if the project had the database changed, we
needed to change the SQL query leading to maintenance problem.
4. Automated table creation: Hibernate framework provides a means to create database
tables automatically. Therefore, there is no need to create tables in the database manually.
5. Simple complex join command: It is possible to get data from multiple tables easily
with Hibernate framework.
6. Provides query statistics and database state: Hibernate supports query cache and
provides statistics about the query and the state of the database [2].
C. How does Hibernate work?
166
1. Hibernate Architecture
As you can see in the diagram below, the amount of Hibernate you use is determined by
your requirements. Hibernate offers a lightweight architecture that only deals with ORM at its
most basic level. Most aspects of persistence, such as transaction, entity/query caching, and
link pooling, are entirely abstracted when Hibernate is used in its entirety.
The success of Hibernate is due to the simplicity of its core concepts. The Hibernate
Session is at the heart of any connection between your code and the database. If you are
familiar with Hibernate, the following picture offers a summary of how it operates and does
not include any clarification.
The configuration is at the core of every Hibernate program. In any Hibernate
application, two pieces of configuration are required: one creates the database connections,
and the other creates the object-to-table mapping.
Let's see the basic introduction to all the components and their roles in hibernate.
2. Configure the Database Connection (.cfg.xml)
In order to create a connection to our database, Hibernate needs to know about our
tables, classes, and other mechanics. This information should be viewed as an XML file
(typically called hibernate.cfg.xml) or a simple text file with name/value pairs (usually named
hibernate.properties).
The data is in XML format. This file is called hibernate.cfg.xml so that it can be loaded
automatically by the framework.
The following snippet describes such a configuration file. Because I am using MySQL
as the database, the connection details for the MySQL database are declared in this
hibernate.cfg.xml file:
167
The preceding properties can also be expressed as name/value pairs.
3.
The Hibernate Session
The Hibernate Session is a persistence service (or persistence manager) that can be used
to query and perform insert, update, and delete operations on Hibernate-mapped class
instances. All of these interactions are performed using object oriented semantics in an ORM
tool; that is, instead of using tables and columns, you use Java classes and object properties.
The Session is a lightweight, short-lived entity that acts as a bridge between the application
and the database during a conversation. The Session wraps the underlying JDBC connection
or J2EE data source, and it serves as a first-level cache for persistent objects bound to it.
4.
The Session Factory
Hibernate allows you to send it the details it needs to link to each database that an
application uses, as well as which classes are mapped to each database. The SessionFactory,
which is used to retrieve Hibernate Sessions, compiles and caches each
of these database-specific configuration files, as well as the corresponding class
mappings. The SessionFactory is a large object that should preferably be generated only once
(due to its high cost and slowness) and made accessible to application code that requires
persistence operations.
Using one of the given Hibernate dialects, each SessionFactory is designed to operate
with a specific database platform. When using database-specific features including native
primary key generation schemes or Session locking, the Hibernate dialect you use is crucial.
At the time of this writing Hibernate (version 3.1) supports 22 database dialects. Each of the
dialect implementations are in the package org.hibernate.dialect
5.
The Hibernate Object Mappings
Hibernate uses an XML configuration file to specify how each object state is retrieved
and stored in the database. Hibernate mappings are loaded and cached in the SessionFactory
at startup. Each mapping specifies a variety of parameters related to the persistence lifecycle
of instances of the mapped class such as:
168
•
Primary key mapping and generation scheme
•
Object-field-to-table-column mappings
•
Associations/Collections
•
Caching settings
•
Custom SQL, store procedure calls, filters, parameterized queries, and more
6.
Persistence Lifecycle of Hibernate
There are three possible states for a Hibernate mapped object. Understanding these
states and the actions that cause state transitions will become very important when dealing
with the more complex Hibernate problems.
The object is not associated with a database table in the transient state. That is, the
object's state has not been saved to a table, and it has no database identity (no primary key has
been assigned). Objects in the transient state are non-transactional, which means they don't
engage in any Hibernate Session-bound transactions. After a successful invocation of the save
or saveOrUpdate methods an object ceases to be transient and becomes persistent. The
Session delete method (or a delete query) produces the inverse effect making a persistent
object transient.
Persistent objects are those that have a database ID. (They are referred to as being in the
"latest" state if they have been allocated a primary key but have not yet been saved to the
database.) Persistent objects are transactional, which means that they take part in the Session's
transactions (at the end of the transaction the object state will be synchronized with the
database).
A disconnected object is a persistent object that is no longer in the Session cache (and
169
hence no longer connected with the Session). When a transaction is completed, the Session is
terminated, cleared, or the object is directly evicted from the Session cache, this occurs.
Objects in the detached state can essentially become inter tier transfer objects, and in some
applications, architectures can replace the need for DTOs, thanks to Hibernate's transparency
when it comes to providing persistent services to an object (Data Transfer Objects).
D. Results of work
By applying Hibernate to the project, we have completed the project with all the
necessary features. Our project is about online banking via a website, built on top of Java-jspservlet. We have divided our project into two main roles that are Admin and Customer.
1. Admin
In Admin roles, there are main seven features for this role. Admin is the person
responsible for managing the bank's data and managing the main tasks of the customer
segment such as opening accounts, locking accounts, or basic online banking operations such
as withdrawing and transferring money online, check transaction information as well as
transaction history. Admin can also view detailed information for a particular account.
Admin can create a new account with necessary information of user. After click submit,
username and password as well as transaction password have been displayed on scree as
below:
170
For closing and viewing a particular account, Admin need to enter account number for
executing.
171
The same for viewing a particular account information:
Apart from the above, deposit and withdraw function require to input account number
and amount to be deposited or withdrawn:
Another feature is Fund Transfer, this function can be handled by entering source account
number, target account number and amount to be transferred:
172
Finally, admin can also check the history information about transactions of particular
account
by
inputting
their
account
number:
2. Customer
On the other hand, Customer features is more limited than Admin with four main
functions.
Firstly, customer can view their account information by click on View Account tab.
Furthermore, customer also can transfer their money to another account within inputting
correct data.
173
After transferring successfully, user can check the transaction history for personal
reasons. Another important point is that customer can change their security information,
which are user name, password and transaction password.
For detail demonstration, we have to represented it in our screencast video.
III. CONCLUSION
In general, Hibernate framework has shown that it is a useful framework for developing
website application with many powerful features. Hibernate is such one of the most suitable
for student project. In this paper, we delivered a brief discussion of the Hibenate and a basic
demo application about Bank Management system.
REFERENCE
[1] Website: Javapoint (2011-2018). Hibernate. />[2] Website: Chandrashekhar (July 2015). Top 10 Advantages of Hibernate.
/>
174