[go: up one dir, main page]

0% found this document useful (0 votes)
35 views55 pages

Aejp Unit IV

Advanced enterprise java programming 4th units
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views55 pages

Aejp Unit IV

Advanced enterprise java programming 4th units
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

UNIT - IV: HIBERNATE

Hibernate - Overview

Hibernate is an Object-Relational Mapping (ORM) solution for JAVA. It is an open source


persistent framework created by Gavin King in 2001. It is a powerful, high performance Object-
Relational Persistence and Query service for any Java Application.

Hibernate maps Java classes to database tables and from Java data types to SQL data types and
relieves the developer from 95% of common data persistence related programming tasks.

Hibernate sits between traditional Java objects and database server to handle all the works in
persisting those objects based on the appropriate O/R mechanisms and patterns.

Hibernate Advantages

 Hibernate takes care of mapping Java classes to database tables using XML files and
without writing any line of code.
 Provides simple APIs for storing and retrieving Java objects directly to and from the
database.
 If there is change in the database or in any table, then you need to change the XML file
properties only.
 Abstracts away the unfamiliar SQL types and provides a way to work around familiar
Java Objects.
 Hibernate does not require an application server to operate.
 Manipulates Complex associations of objects of your database.
 Minimizes database access with smart fetching strategies.
 Provides simple querying of data.

Supported Databases

Hibernate supports almost all the major RDBMS. Following is a list of few of the database
engines supported by Hibernate −

 HSQL Database Engine


 DB2/NT
 MySQL
 PostgreSQL
 FrontBase
 Oracle
 Microsoft SQL Server Database
 Sybase SQL Server
 Informix Dynamic Server

Supported Technologies

Hibernate supports a variety of other technologies, including −

 XDoclet Spring
 J2EE
 Eclipse plug-ins
 Maven

Hibernate - Architecture

Hibernate has a layered architecture which helps the user to operate without having to know the
underlying APIs. Hibernate makes use of the database and configuration data to provide
persistence services (and persistent objects) to the application.

Following is a very high level view of the Hibernate Application Architecture.


Following is a detailed view of the Hibernate Application Architecture with its important core
classes.

Hibernate uses various existing Java APIs, like JDBC, Java Transaction API(JTA), and Java
Naming and Directory Interface (JNDI). JDBC provides a rudimentary level of abstraction of
functionality common to relational databases, allowing almost any database with a JDBC driver
to be supported by Hibernate. JNDI and JTA allow Hibernate to be integrated with J2EE
application servers.

Following section gives brief description of each of the class objects involved in Hibernate
Application Architecture.

Configuration Object

The Configuration object is the first Hibernate object you create in any Hibernate application. It
is usually created only once during application initialization. It represents a configuration or
properties file required by the Hibernate.

The Configuration object provides two keys components −

 Database Connection − This is handled through one or more configuration files


supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml.
 Class Mapping Setup − This component creates the connection between the Java classes
and database tables.

SessionFactory Object

Configuration object is used to create a SessionFactory object which in turn configures Hibernate
for the application using the supplied configuration file and allows for a Session object to be
instantiated. The SessionFactory is a thread safe object and used by all the threads of an
application.

The SessionFactory is a heavyweight object; it is usually created during application start up and
kept for later use. You would need one SessionFactory object per database using a separate
configuration file. So, if you are using multiple databases, then you would have to create
multiple SessionFactory objects.

Session Object

A Session is used to get a physical connection with a database. The Session object is lightweight
and designed to be instantiated each time an interaction is needed with the database. Persistent
objects are saved and retrieved through a Session object.

The session objects should not be kept open for a long time because they are not usually thread
safe and they should be created and destroyed them as needed.

Transaction Object

A Transaction represents a unit of work with the database and most of the RDBMS supports
transaction functionality. Transactions in Hibernate are handled by an underlying transaction
manager and transaction (from JDBC or JTA).

This is an optional object and Hibernate applications may choose not to use this interface, instead
managing transactions in their own application code.

Query Object

Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the
database and create objects. A Query instance is used to bind query parameters, limit the number
of results returned by the query, and finally to execute the query.

Criteria Object

Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.

JPA Object Relational Mapping


Object Relational Mapping (ORM) is a functionality which is used to develop and maintain a
relationship between an object and relational database by mapping an object state to database
column. It is capable to handle various database operations easily such as inserting, updating,
deleting etc.

ORM Frameworks

Following are the various frameworks that function on ORM mechanism: -

 Hibernate
 TopLink
 ORMLite
 iBATIS
 JPOX

Mapping Directions

Mapping Directions are divided into two parts: -

 Unidirectional relationship - In this relationship, only one entity can refer the properties
to another. It contains only one owing side that specifies how an update can be made in
the database.
 Bidirectional relationship - This relationship contains an owning side as well as an
inverse side. So here every entity has a relationship field or refer the property to other
entity.

Types of Mapping

Following are the various ORM mappings:

 One-to-one - This association is represented by @OneToOne annotation. Here, instance


of each entity is related to a single instance of another entity.
 One-to-many - This association is represented by @OneToMany annotation. In this
relationship, an instance of one entity can be related to more than one instance of another
entity.
 Many-to-one - This mapping is defined by @ManyToOne annotation. In this
relationship, multiple instances of an entity can be related to single instance of another
entity.
 Many-to-many - This association is represented by @ManyToMany annotation. Here,
multiple instances of an entity can be related to multiple instances of another entity. In
this mapping, any side can be the owing side.

Collection Mapping in Hibernate

We can map collection elements of Persistent class in Hibernate. You need to declare the type of
collection in Persistent class from one of the following types:

 java.util.List
 java.util.Set
 java.util.SortedSet
 java.util.Map
 java.util.SortedMap
 java.util.Collection
 or write the implementation of org.hibernate.usertype.UserCollectionType

The persistent class should be defined like this for collection element.

1. package com.javatpoint;
2.
3. import java.util.List;
4.
5. public class Question {
6. private int id;
7. private String qname;
8. private List<String> answers;//List can be of any type
9.
10. //getters and setters
11.
12. }

Mapping collection in mapping file

There are many subelements of <class> elements to map the collection. They are <list>, <bag>,
<set> and <map>. Let's see how we implement the list for the above class:

1. <class name="com.javatpoint.Question" table="q100">


2. <id name="id">
3. <generator class="increment"></generator>
4. </id>
5. <property name="qname"></property>
6.
7. <list name="answers" table="ans100">
8. <key column="qid"></key>
9. <index column="type"></index>
10. <element column="answer" type="string"></element>
11. </list>
12.
13. </class>

There are three subelements used in the list:

 <key> element is used to define the foreign key in this table based on the Question class
identifier.
 <index> element is used to identify the type. List and Map are indexed collection.
 <element> is used to define the element of the collection.

This is the mapping of collection if collection stores string objects. But if collection stores entity
reference (another class objects), we need to define <one-to-many> or <many-to-many>
element. Now the Persistent class will look like:

1. package com.javatpoint;
2.
3. import java.util.List;
4.
5. public class Question {
6. private int id;
7. private String qname;
8. private List<Answer> answers;//Here, List stores the objects of Answer class
9.
10. //getters and setters
11.
12. }

1. package com.javatpoint;
2. import java.util.List;
3. public class Answer {
4. private int id;
5. private String answer;
6. private String posterName;
7. //getters and setters
8. }

Now the mapping file will be:

1. <class name="com.javatpoint.Question" table="q100">


2. <id name="id">
3. <generator class="increment"></generator>
4. </id>
5. <property name="qname"></property>
6.
7. <list name="answers" >
8. <key column="qid"></key>
9. <index column="type"></index>
10. <one-to-many class="com.javatpoint.Answer" />
11. </list>
12.
13. </class>

Here, List is mapped by one-to-many relation. In this scenario, there can be many answers for
one question.

Understanding key element

The key element is used to define the foreign key in the joined table based on the original
identity. The foreign key element is nullable by default. So for non-nullable foreign key, we need
to specify not-null attribute such as:

1. <key column="qid" not-null="true" ></key>

The attributes of the key element are column, on-delete, property-ref, not-null, update and
unique.

1. <key
2. column="columnname"
3. on-delete="noaction|cascade"
4. not-null="true|false"
5. property-ref="propertyName"
6. update="true|false"
7. unique="true|false"
8. />

Indexed collections

The collection elements can be categorized in two forms:

 indexed ,and
 non-indexed

The List and Map collection are indexed whereas set and bag collections are non-indexed. Here,
indexed collection means List and Map requires an additional element <index>.
Collection Elements

The collection elements can have value or entity reference (another class object). We can use one
of the 4 elements

 element
 component-element
 one-to-many, or
 many-to-many

The element and component-element are used for normal value such as string, int etc. whereas
one-to-many and many-to-many are used to map entity reference.

Association Mappings
Introduction

Association mappings are often the most difficult thing to implement correctly. In this section we
examine some canonical cases one by one, starting with unidirectional mappings and then
bidirectional cases. We will use Person and Address in all the examples.

Associations will be classified by multiplicity and whether or not they map to an intervening join
table.

Nullable foreign keys are not considered to be good practice in traditional data modelling, so our
examples do not use nullable foreign keys. This is not a requirement of Hibernate, and the
mappings will work if you drop the nullability constraints.

Unidirectional associations

1. Many-to-one

A unidirectional many-to-one association is the most common kind of unidirectional association.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )

2. One-to-one

A unidirectional one-to-one association on a foreign key is almost identical. The only difference
is the column unique constraint.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

A unidirectional one-to-one association on a primary key usually uses a special id generator In


this example, however, we have reversed the direction of the association:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
</class>

<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )

3. One-to-many

A unidirectional one-to-many association on a foreign key is an unusual case, and is not


recommended.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses">
<key column="personId"
not-null="true"/>
<one-to-many class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( addressId bigint not null primary key, personId bigint not null )

You should instead use a join table for this kind of association.

Unidirectional associations with join tables

1. One-to-many

A unidirectional one-to-many association on a join table is the preferred option. Specifying


unique="true", changes the multiplicity from many-to-many to one-to-many.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId not null, addressId bigint not null primary key )
create table Address ( addressId bigint not null primary key )

2. Many-to-one

A unidirectional many-to-one association on a join table is common when the association is


optional. For example:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</join>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )
3. One-to-one

A unidirectional one-to-one association on a join table is possible, but extremely unusual.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId"
unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null
unique )
create table Address ( addressId bigint not null primary key )

4. Many-to-many

Finally, here is an example of a unidirectional many-to-many association.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key
(personId, addressId) )
create table Address ( addressId bigint not null primary key )

4. Bidirectional associations

1. one-to-many / many-to-one

A bidirectional many-to-one association is the most common kind of association. The following
example illustrates the standard parent/child relationship.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="people" inverse="true">
<key column="addressId"/>
<one-to-many class="Person"/>
</set>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )

If you use a List, or other indexed collection, set the key column of the foreign key to not null.
Hibernate will manage the association from the collections side to maintain the index of each
element, making the other side virtually inverse by setting update="false" and insert="false":

<class name="Person">
<id name="id"/>
...
<many-to-one name="address"
column="addressId"
not-null="true"
insert="false"
update="false"/>
</class>

<class name="Address">
<id name="id"/>
...
<list name="people">
<key column="addressId" not-null="true"/>
<list-index column="peopleIdx"/>
<one-to-many class="Person"/>
</list>
</class>

If the underlying foreign key column is NOT NULL, it is important that you define not-
null="true" on the <key> element of the collection mapping. Do not only declare not-null="true"
on a possible nested <column> element, but on the <key> element.

2. One-to-one

A bidirectional one-to-one association on a foreign key is common:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>
create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

A bidirectional one-to-one association on a primary key uses the special id generator:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<one-to-one name="address"/>
</class>

<class name="Address">
<id name="id" column="personId">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person"
constrained="true"/>
</class>
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint not null primary key )

Bidirectional associations with join tables


1. one-to-many / many-to-one

The following is an example of a bidirectional one-to-many association on a join table. The


inverse="true" can go on either end of the association, on the collection, or on the join.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses"
table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
unique="true"
class="Address"/>
</set>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
inverse="true"
optional="true">
<key column="addressId"/>
<many-to-one name="person"
column="personId"
not-null="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null primary key )
create table Address ( addressId bigint not null primary key )

2. one to one

A bidirectional one-to-one association on a join table is possible, but extremely unusual.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true">
<key column="personId"
unique="true"/>
<many-to-one name="address"
column="addressId"
not-null="true"
unique="true"/>
</join>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<join table="PersonAddress"
optional="true"
inverse="true">
<key column="addressId"
unique="true"/>
<many-to-one name="person"
column="personId"
not-null="true"
unique="true"/>
</join>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null primary key, addressId bigint not null
unique )
create table Address ( addressId bigint not null primary key )

3. Many-to-many

Here is an example of a bidirectional many-to-many association.

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<set name="addresses" table="PersonAddress">
<key column="personId"/>
<many-to-many column="addressId"
class="Address"/>
</set>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<set name="people" inverse="true" table="PersonAddress">
<key column="addressId"/>
<many-to-many column="personId"
class="Person"/>
</set>
</class>
create table Person ( personId bigint not null primary key )
create table PersonAddress ( personId bigint not null, addressId bigint not null, primary key
(personId, addressId) )
create table Address ( addressId bigint not null primary key )
More complex association mappings

More complex association joins are extremely rare. Hibernate handles more complex situations
by using SQL fragments embedded in the mapping document. For example, if a table with
historical account information data defines accountNumber, effectiveEndDate and
effectiveStartDatecolumns, it would be mapped as follows:

<properties name="currentAccountKey">
<property name="accountNumber" type="string" not-null="true"/>
<property name="currentAccount" type="boolean">
<formula>case when effectiveEndDate is null then 1 else 0 end</formula>
</property>
</properties>
<property name="effectiveEndDate" type="date"/>
<property name="effectiveStateDate" type="date" not-null="true"/>

You can then map an association to the current instance, the one with null effectiveEndDate, by
using:

<many-to-one name="currentAccountInfo"
property-ref="currentAccountKey"
class="AccountInfo">
<column name="accountNumber"/>
<formula>'1'</formula>
</many-to-one>

In a more complex example, imagine that the association between Employee and Organization is
maintained in an Employment table full of historical employment data. An association to the
employee's most recent employer, the one with the most recent startDate, could be mapped in the
following way:

<join>
<key column="employeeId"/>
<subselect>
select employeeId, orgId
from Employments
group by orgId
having startDate = max(startDate)
</subselect>
<many-to-one name="mostRecentEmployer"
class="Organization"
column="orgId"/>
</join>
This functionality allows a degree of creativity and flexibility, but it is more practical to handle
these kinds of cases using HQL or a criteria query.

Hibernate - Component Mappings

A Component mapping is a mapping for a class having a reference to another class as a member
variable. We have seen such mapping while having two tables and using <set> element in the
mapping file. Now we will use <component> element in the mapping file and a single table
would be used to keep the attributes contained inside the class variable.

Define RDBMS Tables

Consider a situation where we need to store our employee records in EMPLOYEE table which
will have following structure −

create table EMPLOYEE (


id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);

Further, assume each employee will have an address, so let us add address specific fields in the
same table as follows −

create table EMPLOYEE (


id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
street_name VARCHAR(40) default NULL,
city_name VARCHAR(40) default NULL,
state_name VARCHAR(40) default NULL,
zipcode VARCHAR(10) default NULL,
PRIMARY KEY (id)
);
Define POJO Classes

Let us implement our POJO class Employee, which will be used to persist the objects related to
EMPLOYEE table.

import java.util.*;

public class Employee implements java.io.Serializable {


private int id;
private String firstName;
private String lastName;
private int salary;
private Address address;

public Employee() {}

public Employee(String fname, String lname, int salary, Address address ) {


this.firstName = fname;
this.lastName = lname;
this.salary = salary;
this.address = address;
}

public int getId() {


return id;
}

public void setId( int id ) {


this.id = id;
}

public String getFirstName() {


return firstName;
}

public void setFirstName( String first_name ) {


this.firstName = first_name;
}

public String getLastName() {


return lastName;
}

public void setLastName( String last_name ) {


this.lastName = last_name;
}

public int getSalary() {


return salary;
}

public void setSalary( int salary ) {


this.salary = salary;
}

public Address getAddress() {


return address;
}

public void setAddress( Address address ) {


this.address = address;
}
}

We need to define another POJO class corresponding to ADDRESS entity having address related
fields.

import java.util.*;

public class Address{


private int id;
private String street;
private String city;
private String state;
private String zipcode;

public Address() {}

public Address(String street, String city, String state, String zipcode) {


this.street = street;
this.city = city;
this.state = state;
this.zipcode = zipcode;
}

public int getId() {


return id;
}

public void setId( int id ) {


this.id = id;
}

public String getStreet() {


return street;
}

public void setStreet( String street ) {


this.street = street;
}

public String getCity() {


return city;
}
public void setCity( String city ) {
this.city = city;
}

public String getState() {


return state;
}

public void setState( String state ) {


this.state = state;
}

public String getZipcode() {


return zipcode;
}

public void setZipcode( String zipcode ) {


this.zipcode = zipcode;
}

Define Hibernate Mapping File

Let us develop our mapping file, which instructs Hibernate how to map the defined classes to the
database tables. The <component> element will be used to define the rule for all the fields
associated with ADDRESS table.

<?xml version = "1.0" encoding = "utf-8"?>


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

<meta attribute = "class-description">


This class contains the employee detail.
</meta>

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<component name = "address" class="Address">


<property name = "street" column = "street_name" type = "string"/>
<property name = "city" column = "city_name" type = "string"/>
<property name = "state" column = "state_name" type = "string"/>
<property name = "zipcode" column = "zipcode" type = "string"/>
</component>

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>

</class>
</hibernate-mapping>

You should save the mapping document in a file with the format <classname>.hbm.xml. We
saved our mapping document in the file Employee.hbm.xml. You are already familiar with most
of the mapping detail, but let us see all the elements of mapping file once again −

 The mapping document is an XML document having <hibernate-mapping> as the root


element which contains two <class> elements corresponding to each class.
 The <class> elements are used to define specific mappings from a Java classes to the
database tables. The Java class name is specified using the name attribute of the class
element and the database table name is specified using the table attribute.
 The <meta> element is optional element and can be used to create the class description.
 The <id> element maps the unique ID attribute in class to the primary key of the database
table. The name attribute of the id element refers to the property in the class and the
column attribute refers to the column in the database table. The type attribute holds the
hibernate mapping type, this mapping types will convert from Java to SQL data type.
 The <generator> element within the id element is used to generate the primary key
values automatically. The class attribute of the generator element is set to native to let
hibernate pick up either identity, sequence or hilo algorithm to create primary key
depending upon the capabilities of the underlying database.
 The <property> element is used to map a Java class property to a column in the database
table. The name attribute of the element refers to the property in the class and the
column attribute refers to the column in the database table. The type attribute holds the
hibernate mapping type, this mapping types will convert from Java to SQL data type.
 The <component> element sets the existence of different attributes of Address class
inside Employee classes.

Create Application Class

Finally, we will create our application class with the main() method to run the application. We
will use this application to save few Employee's records along with their certificates and then we
will apply CRUD operations on those records.

import java.util.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {


private static SessionFactory factory;
public static void main(String[] args) {

try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}

ManageEmployee ME = new ManageEmployee();

/* Let us have one address object */


Address address1 = ME.addAddress("Kondapur","Hyderabad","AP","532");

/* Add employee records in the database */


Integer empID1 = ME.addEmployee("Manoj", "Kumar", 4000, address1);

/* Let us have another address object */


Address address2 = ME.addAddress("Saharanpur","Ambehta","UP","111");

/* Add another employee record in the database */


Integer empID2 = ME.addEmployee("Dilip", "Kumar", 3000, address2);

/* List down all the employees */


ME.listEmployees();

/* Update employee's salary records */


ME.updateEmployee(empID1, 5000);

/* List down all the employees */


ME.listEmployees();

/* Method to add an address record in the database */


public Address addAddress(String street, String city, String state, String zipcode) {
Session session = factory.openSession();
Transaction tx = null;
Integer addressID = null;
Address address = null;

try {
tx = session.beginTransaction();
address = new Address(street, city, state, zipcode);
addressID = (Integer) session.save(address);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return address;
}

/* Method to add an employee record in the database */


public Integer addEmployee(String fname, String lname, int salary, Address address){
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;

try {
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary, address);
employeeID = (Integer) session.save(employee);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return employeeID;
}

/* Method to list all the employees detail */


public void listEmployees( ){
Session session = factory.openSession();
Transaction tx = null;

try {
tx = session.beginTransaction();
List employees = session.createQuery("FROM Employee").list();
for (Iterator iterator = employees.iterator(); iterator.hasNext();){
Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
Address add = employee.getAddress();
System.out.println("Address ");
System.out.println("\tStreet: " + add.getStreet());
System.out.println("\tCity: " + add.getCity());
System.out.println("\tState: " + add.getState());
System.out.println("\tZipcode: " + add.getZipcode());
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}

/* Method to update salary for an employee */


public void updateEmployee(Integer EmployeeID, int salary ){
Session session = factory.openSession();
Transaction tx = null;

try {
tx = session.beginTransaction();
Employee employee = (Employee)session.get(Employee.class, EmployeeID);
employee.setSalary( salary );
session.update(employee);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
Compilation and Execution

Here are the steps to compile and run the above mentioned application. Make sure, you have set
PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

 Create hibernate.cfg.xml configuration file as explained in configuration chapter.


 Create Employee.hbm.xml mapping file as shown above.
 Create Employee.java source file as shown above and compile it.
 Create ManageEmployee.java source file as shown above and compile it.
 Execute ManageEmployee binary to run the program.
You would get the following result on the screen, and same time records would be created in
EMPLOYEE table.

$java ManageEmployee
.......VARIOUS LOG MESSAGES WILL DISPLAY HERE........

First Name: Manoj Last Name: Kumar Salary: 4000


Address
Street: Kondapur
City: Hyderabad
State: AP
Zipcode: 532
First Name: Dilip Last Name: Kumar Salary: 3000
Address
Street: Saharanpur
City: Ambehta
State: UP
Zipcode: 111
First Name: Manoj Last Name: Kumar Salary: 5000
Address
Street: Kondapur
City: Hyderabad
State: AP
Zipcode: 532
First Name: Dilip Last Name: Kumar Salary: 3000
Address
Street: Saharanpur
City: Ambehta
State: UP
Zipcode: 111

If you check your EMPLOYEE table, it should have following records −

mysql> select id, first_name,salary, street_name, state_name from EMPLOYEE;


+----+------------+--------+-------------+------------+
| id | first_name | salary | street_name | state_name |
+----+------------+--------+-------------+------------+
| 1 | Manoj | 5000 | Kondapur | AP |
| 2 | Dilip | 3000 | Saharanpur | UP |
+----+------------+--------+-------------+------------+
2 rows in set (0.00 sec)

mysql>
Hibernate Inheritance Mapping
In this tutorial, we will discuss how JPA/Hibernate supports Inheritance mapping. We also look
into several inheritance strategies that JPA specification provides.

Relational databases don’t have a straightforward way to map class hierarchies onto database
tables.
To address this, the JPA specification provides several strategies:

1. MappedSuperclass - Inheritance is implemented in the domain model only without


reflecting it in the database schema. In this strategy, the parent classes can’t be entities.
2. Single table - The domain model class hierarchy is materialized into a single table which
contains entities belonging to different class types.
3. Joined table - The base class and all the subclasses have their own database tables and
fetching a subclass entity requires a join with the parent table as well.
4. Table per class - Each subclass has its own table containing both the subclass and the
base class properties.

Entity inheritance means that we can use polymorphic queries for retrieving all the sub-
class entities when querying for a super-class.

1. Hibernate/JPA - MappedSuperclass Inheritance


The JPA standard specification defines the @MappedSuperclass annotation to allow an entity to
inherit properties from a base class.

From a database perspective, the @MappedSuperclass inheritance model is invisible since all the
base class properties are simply copied to the database table mapped by the actual entity class.

Example: @MappedSuperclass inheritance

In the following domain model class hierarchy, a DebitAccount and a CreditAccount share the
same Account base class.
@MappedSuperclass
public static class Account {

@Id
private Long id;

private String owner;

private BigDecimal balance;

private BigDecimal interestRate;

//Getters and setters are omitted for brevity

@Entity(name = "DebitAccount")
public static class DebitAccount extends Account {

private BigDecimal overdraftFee;

//Getters and setters are omitted for brevity

@Entity(name = "CreditAccount")
public static class CreditAccount extends Account {

private BigDecimal creditLimit;

//Getters and setters are omitted for brevity

}
CREATE TABLE DebitAccount (
id BIGINT NOT NULL ,
balance NUMERIC(19, 2) ,
interestRate NUMERIC(19, 2) ,
owner VARCHAR(255) ,
overdraftFee NUMERIC(19, 2) ,
PRIMARY KEY ( id )
)

CREATE TABLE CreditAccount (


id BIGINT NOT NULL ,
balance NUMERIC(19, 2) ,
interestRate NUMERIC(19, 2) ,
owner VARCHAR(255) ,
creditLimit NUMERIC(19, 2) ,
PRIMARY KEY ( id )
)

Read more about MappedSuperclass inheritance strategy with example at Hibernate/JPA


MappedSuperclass Inheritance Example

2. Hibernate/JPA Single Table Inheritance


The single table strategy maps all entities of the inheritance structure to the same database table.
This approach makes polymorphic queries very efficient and provides the best performance.

SINGLE_TABLE inheritance performs the best in terms of executed SQL statements. However,
you cannot use NOT NULL constraints on the column-level. You can still use triggers and rules
to enforce such constraints, but it’s not as straightforward.

Single Table Inheritance Example

Each subclass in a hierarchy must define a unique discriminator value, which is used to
differentiate between rows belonging to separate subclass types. If this is not specified,
the DTYPE column is used as a discriminator, storing the associated subclass name.
The inheritance strategy is defined on the abstract superclass, using the @Inheritance annotation.
In this example, we used InheritanceType.SINGLE_TABLE. This means all concrete subclasses
will be stored in one table. You can optionally specify a discriminator column name. This
column is registered by the @DiscriminatorColumnif omitted the default DTYPE name is used.

Read complete example at Hibernate/JPA Single Table Inheritance Example

3. Hibernate/JPA Joined Table Inheritance


Each subclass can also be mapped to its own table. This is also called table-per-subclass
mapping strategy. An inherited state is retrieved by joining with the table of the superclass.

A discriminator column is not required for this mapping strategy. Each subclass must, however,
declare a table column holding the object identifier.

The JOINED table inheritance strategy addresses the data integrity concerns because every
subclass is associated with a different table. Polymorphic queries or @OneToMany base class
associations don’t perform very well with this strategy. However, polymorphic @ManyToOne
associations are fine, and they can provide a lot of value.

Joined Table Inheritance Example


Let's use @Inheritance(strategy = InheritanceType.JOINED) annotation to use this strategy.

package net.javaguides.hibernate.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Account {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String owner;

private double balance;

private double interestRate;

Demonstrated SINGLE_TABLE inheritance strategy with a complete hibernate example at


Hibernate JPA Joined Table Inheritance Example

4. Hibernate/JPA Table Per Class Inheritance


In a Table per class inheritance strategy, each concrete subclass has its own table containing both
the subclass and the base class properties.

Example: Use @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

package net.javaguides.hibernate.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@Entity(name = "Account")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Account {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String owner;

private double balance;

private double interestRate;

Hibernate Lifecycle

 Difficulty Level : Medium


 Last Updated : 07 Oct, 2021

Here we will learn about Hibernate Lifecycle or in other words, we can say that we will learn
about the lifecycle of the mapped instances of the entity/object classes in hibernate. In Hibernate,
we can either create a new object of an entity and store it into the database, or we can fetch the
existing data of an entity from the database. These entity is connected with the lifecycle and each
object of entity passes through the various stages of the lifecycle.

There are mainly four states of the Hibernate Lifecycle :

1. Transient State
2. Persistent State
3. Detached State
4. Removed State
Hibernate Lifecycle

As depicted from the above media one can co-relate how they are plotted in order to plot better
in our mind. Now we will be discussing the states to better interpret hibernate lifecycle. It is as
follows:

State 1: Transient State

The transient state is the first state of an entity object. When we instantiate an object of a POJO
class using the new operator then the object is in the transient state. This object is not connected
with any hibernate session. As it is not connected to any Hibernate Session, So this state is not
connected to any database table. So, if we make any changes in the data of the POJO Class then
the database table is not altered. Transient objects are independent of Hibernate, and they exist in
the heap memory.

Changing new object to Transient State

There are two layouts in which transient state will occur as follows:

1. When objects are generated by an application but are not connected to any session.
2. The objects are generated by a closed session.

Here, we are creating a new object for the Employee class. Below is the code which shows the
initialization of the Employee object :
//Here, The object arrives in the transient state.
Employee e = new Employee();
e.setId(21);
e.setFirstName("Neha");
e.setMiddleName("Shri");
e.setLastName("Rudra");

State 2: Persistent State

Once the object is connected with the Hibernate Session then the object moves into the Persistent
State. So, there are two ways to convert the Transient State to the Persistent State :

1. Using the hibernated session, save the entity object into the database table.
2. Using the hibernated session, load the entity object into the database table.

In this state. each object represents one row in the database table. Therefore, if we make any
changes in the data then hibernate will detect these changes and make changes in the database
table.

Converting Transient State to Persistent State

Following are the methods given for the persistent state:

 session.persist(e);
 session.save(e);
 session.saveOrUpdate(e);
 session.update(e);
 session.merge(e);
 session.lock(e);

Example:

// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);

//Persistent State
session.save(e);
State 3: Detached State

For converting an object from Persistent State to Detached State, we either have to close the
session or we have to clear its cache. As the session is closed here or the cache is cleared, then
any changes made to the data will not affect the database table. Whenever needed, the detached
object can be reconnected to a new hibernate session. To reconnect the detached object to a new
hibernate session, we will use the following methods as follows:

 merge()
 update()
 load()
 refresh()
 save()
 update()

Following are the methods used for the detached state :

 session.detach(e);
 session.evict(e);
 session.clear();
 session.close();

Converting Persistent State to Detached State

Example

// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);

// Persistent State
session.save(e);

// Detached State
session.close();

State 4: Removed State


In the hibernate lifecycle it is the last state. In the removed state, when the entity object is deleted
from the database then the entity object is known to be in the removed state. It is done by calling
the delete() operation. As the entity object is in the removed state, if any change will be done in
the data will not affect the database table.

Note: To make a removed entity object we will call session.delete().

Converting Persistent State to Removed State

Example

// Java Pseudo code to Illustrate Remove State

// Transient State
Employee e = new Employee();
Session s = sessionfactory.openSession();
e.setId(01);

// Persistent State
session.save(e)

// Removed State
session.delete(e);

Hibernate Transaction Management Example

A transaction simply represents a unit of work. In such case, if one step fails, the whole
transaction fails (which is termed as atomicity). A transaction can be described by ACID
properties (Atomicity, Consistency, Isolation and Durability).
Transaction Interface in Hibernate

In hibernate framework, we have Transaction interface that defines the unit of work. It
maintains abstraction from the transaction implementation (JTA,JDBC).

A transaction is associated with Session and instantiated by calling session.beginTransaction().

The methods of Transaction interface are as follows:

1. void begin() starts a new transaction.


2. void commit() ends the unit of work unless we are in FlushMode.NEVER.
3. void rollback() forces this transaction to rollback.
4. void setTimeout(int seconds) it sets a transaction timeout for any transaction started by
a subsequent call to begin on this instance.
5. boolean isAlive() checks if the transaction is still alive.
6. void registerSynchronization(Synchronization s) registers a user synchronization
callback for this transaction.
7. boolean wasCommited() checks if the transaction is commited successfully.
8. boolean wasRolledBack() checks if the transaction is rolledback successfully.

Example of Transaction Management in Hibernate

In hibernate, it is better to rollback the transaction if any exception occurs, so that resources can
be free. Let's see the example of transaction management in hibernate.

1. Session session = null;


2. Transaction tx = null;
3.
4. try {
5. session = sessionFactory.openSession();
6. tx = session.beginTransaction();
7. //some action
8.
9. tx.commit();
10.
11. }catch (Exception ex) {
12. ex.printStackTrace();
13. tx.rollback();
14. }
15. finally {session.close();}

Hibernate Query Language (HQL)

1. Hibernate Query Language


2. Advantage of HQL
3. Query Interface

Hibernate Query Language (HQL) is same as SQL (Structured Query Language) but it doesn't
depends on the table of the database. Instead of table name, we use class name in HQL. So it is
database independent query language.

Advantage of HQL

There are many advantages of HQL. They are as follows:

 database independent
 supports polymorphic queries
 easy to learn for Java Programmer

Query Interface

It is an object oriented representation of Hibernate Query. The object of Query can be obtained
by calling the createQuery() method Session interface.

The query interface provides many methods. There is given commonly used methods:

1. public int executeUpdate() is used to execute the update or delete query.


2. public List list() returns the result of the ralation as a list.
3. public Query setFirstResult(int rowno) specifies the row number from where record
will be retrieved.
4. public Query setMaxResult(int rowno) specifies the no. of records to be retrieved from
the relation (table).
5. public Query setParameter(int position, Object value) it sets the value to the JDBC
style query parameter.
6. public Query setParameter(String name, Object value) it sets the value to a named
query parameter.
Example of HQL to get all the records

1. Query query=session.createQuery("from Emp");//here persistent class name is Emp


2. List list=query.list();

Example of HQL to get records with pagination

1. Query query=session.createQuery("from Emp");


2. query.setFirstResult(5);
3. query.setMaxResult(10);
4. List list=query.list();//will return the records from 5 to 10th number

Example of HQL update query

1. Transaction tx=session.beginTransaction();
2. Query q=session.createQuery("update User set name=:n where id=:i");
3. q.setParameter("n","Udit Kumar");
4. q.setParameter("i",111);
5.
6. int status=q.executeUpdate();
7. System.out.println(status);
8. tx.commit();

Example of HQL delete query

1. Query query=session.createQuery("delete from Emp where id=100");


2. //specifying class name (Emp) not tablename
3. query.executeUpdate();

HQL with Aggregate functions

You may call avg(), min(), max() etc. aggregate functions by HQL. Let's see some common
examples:

Example to get total salary of all the employees

1. Query q=session.createQuery("select sum(salary) from Emp");


2. List<Integer> list=q.list();
3. System.out.println(list.get(0));

Example to get maximum salary of employee

1. Query q=session.createQuery("select max(salary) from Emp");


Example to get minimum salary of employee

1. Query q=session.createQuery("select min(salary) from Emp");

Example to count total number of employee ID

1. Query q=session.createQuery("select count(id) from Emp");

Example to get average salary of each employees

1. Query q=session.createQuery("select avg(salary) from Emp");

Hibernate - Native SQL

You can use native SQL to express database queries if you want to utilize database-specific
features such as query hints or the CONNECT keyword in Oracle. Hibernate 3.x allows you to
specify handwritten SQL, including stored procedures, for all create, update, delete, and load
operations.

Your application will create a native SQL query from the session with the createSQLQuery()
method on the Session interface −

public SQLQuery createSQLQuery(String sqlString) throws HibernateException

After you pass a string containing the SQL query to the createSQLQuery() method, you can
associate the SQL result with either an existing Hibernate entity, a join, or a scalar result using
addEntity(), addJoin(), and addScalar() methods respectively.

Scalar Queries

The most basic SQL query is to get a list of scalars (values) from one or more tables. Following
is the syntax for using native SQL for scalar values −

String sql = "SELECT first_name, salary FROM EMPLOYEE";


SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List results = query.list();
Entity Queries

The above queries were all about returning scalar values, basically returning the "raw" values
from the result set. Following is the syntax to get entity objects as a whole from a native sql
query via addEntity().

String sql = "SELECT * FROM EMPLOYEE";


SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
List results = query.list();
Named SQL Queries

Following is the syntax to get entity objects from a native sql query via addEntity() and using
named SQL query.

String sql = "SELECT * FROM EMPLOYEE WHERE id = :employee_id";


SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
query.setParameter("employee_id", 10);
List results = query.list();
Native SQL Example

Consider the following POJO class −

public class Employee {


private int id;
private String firstName;
private String lastName;
private int salary;

public Employee() {}

public Employee(String fname, String lname, int salary) {


this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}

public int getId() {


return id;
}

public void setId( int id ) {


this.id = id;
}

public String getFirstName() {


return firstName;
}

public void setFirstName( String first_name ) {


this.firstName = first_name;
}
public String getLastName() {
return lastName;
}

public void setLastName( String last_name ) {


this.lastName = last_name;
}

public int getSalary() {


return salary;
}

public void setSalary( int salary ) {


this.salary = salary;
}
}

Let us create the following EMPLOYEE table to store Employee objects −

create table EMPLOYEE (


id INT NOT NULL auto_increment,
first_name VARCHAR(20) default NULL,
last_name VARCHAR(20) default NULL,
salary INT default NULL,
PRIMARY KEY (id)
);

Following will be mapping file −

<?xml version = "1.0" encoding = "utf-8"?>


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name = "Employee" table = "EMPLOYEE">

<meta attribute = "class-description">


This class contains the employee detail.
</meta>

<id name = "id" type = "int" column = "id">


<generator class="native"/>
</id>

<property name = "firstName" column = "first_name" type = "string"/>


<property name = "lastName" column = "last_name" type = "string"/>
<property name = "salary" column = "salary" type = "int"/>

</class>
</hibernate-mapping>

Finally, we will create our application class with the main() method to run the application where
we will use Native SQL queries −

import java.util.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.SQLQuery;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;

public class ManageEmployee {


private static SessionFactory factory;
public static void main(String[] args) {

try {
factory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}

ManageEmployee ME = new ManageEmployee();

/* Add few employee records in database */


Integer empID1 = ME.addEmployee("Zara", "Ali", 2000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 5000);
Integer empID4 = ME.addEmployee("Mohd", "Yasee", 3000);

/* List down employees and their salary using Scalar Query */


ME.listEmployeesScalar();

/* List down complete employees information using Entity Query */


ME.listEmployeesEntity();
}

/* Method to CREATE an employee in the database */


public Integer addEmployee(String fname, String lname, int salary){
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;

try {
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return employeeID;
}

/* Method to READ all the employees using Scalar Query */


public void listEmployeesScalar( ){
Session session = factory.openSession();
Transaction tx = null;

try {
tx = session.beginTransaction();
String sql = "SELECT first_name, salary FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List data = query.list();

for(Object object : data) {


Map row = (Map)object;
System.out.print("First Name: " + row.get("first_name"));
System.out.println(", Salary: " + row.get("salary"));
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}

/* Method to READ all the employees using Entity Query */


public void listEmployeesEntity( ){
Session session = factory.openSession();
Transaction tx = null;

try {
tx = session.beginTransaction();
String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
List employees = query.list();

for (Iterator iterator = employees.iterator(); iterator.hasNext();){


Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
}
tx.commit();
} catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
Compilation and Execution

Here are the steps to compile and run the above mentioned application. Make sure, you have set
PATH and CLASSPATH appropriately before proceeding for the compilation and execution.

 Create hibernate.cfg.xml configuration file as explained in configuration chapter.


 Create Employee.hbm.xml mapping file as shown above.
 Create Employee.java source file as shown above and compile it.
 Create ManageEmployee.java source file as shown above and compile it.
 Execute ManageEmployee binary to run the program.

You would get the following result, and records would be created in the EMPLOYEE table.

$java ManageEmployee
.......VARIOUS LOG MESSAGES WILL DISPLAY HERE........

First Name: Zara, Salary: 2000


First Name: Daisy, Salary: 5000
First Name: John, Salary: 5000
First Name: Mohd, Salary: 3000
First Name: Zara Last Name: Ali Salary: 2000
First Name: Daisy Last Name: Das Salary: 5000
First Name: John Last Name: Paul Salary: 5000
First Name: Mohd Last Name: Yasee Salary: 3000

If you check your EMPLOYEE table, it should have the following records −

mysql> select * from EMPLOYEE;


+----+------------+-----------+--------+
| id | first_name | last_name | salary |
+----+------------+-----------+--------+
| 26 | Zara | Ali | 2000 |
| 27 | Daisy | Das | 5000 |
| 28 | John | Paul | 5000 |
| 29 | Mohd | Yasee | 3000 |
+----+------------+-----------+--------+
4 rows in set (0.00 sec)
mysql>

Hibernate - Query Language

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but
instead of operating on tables and columns, HQL works with persistent objects and their
properties. HQL queries are translated by Hibernate into conventional SQL queries, which in
turns perform action on database.

Although you can use SQL statements directly with Hibernate using Native SQL, but I would
recommend to use HQL whenever possible to avoid database portability hassles, and to take
advantage of Hibernate's SQL generation and caching strategies.

Keywords like SELECT, FROM, and WHERE, etc., are not case sensitive, but properties like
table and column names are case sensitive in HQL.

FROM Clause

You will use FROM clause if you want to load a complete persistent objects into memory.
Following is the simple syntax of using FROM clause −

String hql = "FROM Employee";


Query query = session.createQuery(hql);
List results = query.list();

If you need to fully qualify a class name in HQL, just specify the package and class name as
follows −

String hql = "FROM com.hibernatebook.criteria.Employee";


Query query = session.createQuery(hql);
List results = query.list();
AS Clause

The AS clause can be used to assign aliases to the classes in your HQL queries, especially when
you have the long queries. For instance, our previous simple example would be the following −

String hql = "FROM Employee AS E";


Query query = session.createQuery(hql);
List results = query.list();

The AS keyword is optional and you can also specify the alias directly after the class name, as
follows −

String hql = "FROM Employee E";


Query query = session.createQuery(hql);
List results = query.list();
SELECT Clause

The SELECT clause provides more control over the result set then the from clause. If you want
to obtain few properties of objects instead of the complete object, use the SELECT clause.
Following is the simple syntax of using SELECT clause to get just first_name field of the
Employee object −

String hql = "SELECT E.firstName FROM Employee E";


Query query = session.createQuery(hql);
List results = query.list();

It is notable here that Employee.firstName is a property of Employee object rather than a field
of the EMPLOYEE table.

WHERE Clause

If you want to narrow the specific objects that are returned from storage, you use the WHERE
clause. Following is the simple syntax of using WHERE clause −

String hql = "FROM Employee E WHERE E.id = 10";


Query query = session.createQuery(hql);
List results = query.list();
ORDER BY Clause

To sort your HQL query's results, you will need to use the ORDER BY clause. You can order
the results by any property on the objects in the result set either ascending (ASC) or descending
(DESC). Following is the simple syntax of using ORDER BY clause −

String hql = "FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC";
Query query = session.createQuery(hql);
List results = query.list();

If you wanted to sort by more than one property, you would just add the additional properties to
the end of the order by clause, separated by commas as follows −

String hql = "FROM Employee E WHERE E.id > 10 " +


"ORDER BY E.firstName DESC, E.salary DESC ";
Query query = session.createQuery(hql);
List results = query.list();
GROUP BY Clause

This clause lets Hibernate pull information from the database and group it based on a value of an
attribute and, typically, use the result to include an aggregate value. Following is the simple
syntax of using GROUP BY clause −

String hql = "SELECT SUM(E.salary), E.firtName FROM Employee E " +


"GROUP BY E.firstName";
Query query = session.createQuery(hql);
List results = query.list();
Using Named Parameters

Hibernate supports named parameters in its HQL queries. This makes writing HQL queries that
accept input from the user easy and you do not have to defend against SQL injection attacks.
Following is the simple syntax of using named parameters −

String hql = "FROM Employee E WHERE E.id = :employee_id";


Query query = session.createQuery(hql);
query.setParameter("employee_id",10);
List results = query.list();
UPDATE Clause

Bulk updates are new to HQL with Hibernate 3, and delete work differently in Hibernate 3 than
they did in Hibernate 2. The Query interface now contains a method called executeUpdate() for
executing HQL UPDATE or DELETE statements.

The UPDATE clause can be used to update one or more properties of an one or more objects.
Following is the simple syntax of using UPDATE clause −

String hql = "UPDATE Employee set salary = :salary " +


"WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("salary", 1000);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
DELETE Clause

The DELETE clause can be used to delete one or more objects. Following is the simple syntax
of using DELETE clause −

String hql = "DELETE FROM Employee " +


"WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
INSERT Clause

HQL supports INSERT INTO clause only where records can be inserted from one object to
another object. Following is the simple syntax of using INSERT INTO clause −

String hql = "INSERT INTO Employee(firstName, lastName, salary)" +


"SELECT firstName, lastName, salary FROM old_employee";
Query query = session.createQuery(hql);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
Aggregate Methods

HQL supports a range of aggregate methods, similar to SQL. They work the same way in HQL
as in SQL and following is the list of the available functions −

Sr.No. Functions & Description

avg(property name)
1
The average of a property's value
count(property name or *)
2
The number of times a property occurs in the results
max(property name)
3
The maximum value of the property values
min(property name)
4
The minimum value of the property values
5 sum(property name)
The sum total of the property values

The distinct keyword only counts the unique values in the row set. The following query will
return only unique count −

String hql = "SELECT count(distinct E.firstName) FROM Employee E";


Query query = session.createQuery(hql);
List results = query.list();
Pagination using Query

There are two methods of the Query interface for pagination.

Sr.No. Method & Description

Query setFirstResult(int startPosition)


1
This method takes an integer that represents the first row in your result set, starting
with row 0.
Query setMaxResults(int maxResult)
2
This method tells Hibernate to retrieve a fixed number maxResults of objects.

Using above two methods together, we can construct a paging component in our web or Swing
application. Following is the example, which you can extend to fetch 10 rows at a time −

String hql = "FROM Employee";


Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(10);
List results = query.list();

Hibernate – Query Language

Hibernate is a Java framework that makes it easier to create database-interactive Java


applications. In HQL, instead of a table name, it uses a class name. As a result, it is a query
language that is database-independent. Hibernate converts HQL queries into SQL queries, which
are then used to perform database actions. Although Native SQL may be used directly with
Hibernate, it is encouraged to utilize HQL wherever feasible to prevent database portability
issues.

HQL has many benefits. Some benefits are – they are database independent, polymorphic queries
supported, and easy to learn for Java programmers.
The Query interface provides object-oriented methods and capabilities for representing and
manipulating HQL queries.

Example: HQL FROM Clause

To load a whole persistent object into memory, the FROM clause is used.

String hib = "FROM Student";


Query query = session.createQuery(hib);
List results = query.list();

Example: HQL SELECT Clause

The SELECT clause is used when only a few attributes of an object are required rather than the
entire object.

String hib = "SELECT S.roll FROM Student S";


Query query = session.createQuery(hib);
List results = query.list();

Example: HQL WHERE Clause

Filtering records is done with the WHERE clause. It’s used to retrieve only the records that meet
a set of criteria.

String hib = "FROM Student S WHERE S.id = 5";


Query query = session.createQuery(hib);
List results = query.list();

Example: HQL ORDER BY Clause

The ORDER BY clause is used to sort the results of an HQL query.

String hib = "FROM Student S WHERE S.id > 5 ORDER BY S.id DESC";
Query query = session.createQuery(hib);
List results = query.list();
HQL UPDATE Clause

The UPDATE clause is required to update the value of an attribute.

String hib = "UPDATE Student set name=:n WHERE roll=:i";


Query q=session.createQuery(hib);
q.setParameter("n","John");
q.setParameter("i",23);
int status=q.executeUpdate();
System.out.println(status);
HQL DELETE Clause

It is required to delete a value of an attribute.

String hib = "DELETE FROM Student where id=10";


Query query=session.createQuery(hib);
query.executeUpdate();
HQL INSERT Clause

It is required to Insert values into the relation.

String hib = "insert into Student(first_name, last_name)" +


"select first_name, last_name from backup_student";
Query query = session.createQuery(hib);
int result = query.executeUpdate();
Pagination using Query
Method Action Performed

Query setMaxResults(int
Instructs Hibernate to get a specific number of items
max)

Query setFirstResult(int Takes an integer as an argument that represents the first row in
starting_no) your result set, beginning with row 0.

Example

String hib = "from Student"


Query query=session.createQuery(hib);
query.setFirstResult(5);
query.setMaxResult(10);
List list=query.list();

The above example returns the record from 5 to 10.

Aggregate Methods

Similar to SQL, HQL has a number of aggregation techniques.

Example 1: average

String hib = "select avg(marks) from Student";


Query q=session.createQuery(hib);

Example 2: max

String hib = "select max(marks) from Student";


Query q=session.createQuery(hib);

Example 3: min

String hib = "select min(marks) from Student";


Query q=session.createQuery(hib);

Example 4: count

String hib = "select count(id) from Student";


Query q=session.createQuery(hib);
Example of sum
String hib = "select sum(marks) from Student";
Query q=session.createQuery(hib);
List<Integer> list=q.list();
System.out.println(list.get(0));

You might also like