Spring Hibernate Integration Example – CRUD Operations

Spring Hibernate Integration Example – CRUD Operations
In this article, We are going to see how to perform CRUD Operations with the help of Spring and Hibernate. We are going to use Spring Dependency Injection concept and we are also going to declare Hibernate Properties in Spring Application Context XML file.

I have downloaded and used some jar files in this application,
Jar files Needed:
org.apache.commons.dbcp.jar
commons-pool-1.6.jar
Other than this, Spring Framework, Hibernate and MySQL JDBC Driver are required

Here is the application Structure,
structure

javainfinite.pojo

javainfinite.DAO

javainfinite.DAOImpl

applicationContext.xml

javainfinite.execution

Output:

op1 op2

 Download Source code (Src Files – applicationContext.xml, pojo, DAO, DAOImpl and execution)

 

 

 

Read More

Hibernate Interview Questions – Part 6

Hibernate Interview Questions – Part 6
Implementing Concurrency

What is Concurrency?
Each transaction is Hibernate is isolated from another transaction. So one transaction is not aware of another transaction. Which results in high possibility of a same record in database may be accessed by two or more transactions at same time. When multiple transactions access the same record in database and make changes concurrency issue occurs as data becomes inconsistent.

Concurrency Issues:
Dirty Read:
A transaction reads the data that has not been committed by another transaction

Lost Updates:
When two or more transactions try to access same row this issue occurs. Transactions overwrite the data on same row again and again leading to loss of data manipulation

Nonrepeatable Read:
Transaction reads the same row more than once and another transaction changes the data in the row between the successive reads of the transaction

Phantom Read:
A transaction reads a rowset more than once and another transaction inserts or deletes a row between the successive reads.


How to Control Concurrency Issues?
Setting transaction isolation levels
Implementing versioning
Implementing Locks


Setting Transaction Isolation Levels:
Read Uncommitted: 

In this level, a transaction can read the data modified by another transaction. But a transaction can read the data modified by another transaction even before the commit is made. Dirty Reads, Lost Updates, Nonrepeatable Read and Phantom read all issues occur at this level
Isolation Level – 1

Read Committed:
This is the default Isolation level in SQL database. In this level, When a transaction modifies a data other transactions will not be able to read the modified data. This prevents Dirty Reads but Lost Updates, Nonrepeatable Read and Phantom Read occurs at this level
Isolation Level – 2

Repeatable Read:
In this level, When a transaction is modifying a data no other transactions can Read or Update the data until the current transaction completes it process. At this level, Dirty Reads, Lost Updates and Nonrepeatable Read are prevented but Phantom Read may occur
Isolation Level – 4

Serializable:
In this level, When a transaction is modifying a data no other transaction can read, update, modify or insert data until the current transaction completes its process and commits.
Isolation Level – 8

1


Implementing Versioning:
In this method, version information is added to the persistent entities. Each entity has a version that represents the state of that entity. These version information are stored in a column in a database or timestamp that shows the time the data last modified.

How this Works?
When a record is modified in the database, hibernate increments the version number or timestamp of that record. So when another transaction tries to update the data, Hibernate will check for the version number or timestamp with the version or Timestamp of the transaction and throws exception it the transaction carries older version number


Implementing Locks:
We can implement the Locks when data’s are retrieved from database or when updating in database. Session interface provides lock() method.

LockMode.READ:
This acquires  lock when record is read from database

LockMode.UPGRADE:
This acquires database-level lock for persistent objects that are going to update.

LockMode.WRITE:
This acquires  lock when data is updated or inserted


 

Read More

Hibernate Database Operations – FirstResult and MaxResult

Hibernate Database Operations – FirstResult and MaxResult

In this article we are going to see FirstResult and MaxResult in hibernate.

FirstResult – Suppose if we like to retrieve records from database from 6th record, then we can set setFirstResult(6).

MaxResult – Suppose if we need to retrieve only 10 records from database, then we can use setMaxResult(10).

FetchSize – This is almost similar to MaxResult, We can set number of rows to be retrieved. The difference is that optimization, Change in way of sending the results (Like chunks, buffered etc) – Not Supported by Most of the Database Drivers.

Now let us see an example,

structure1

hibernate.cfg.xml

Employee.java

NewHibernateUtil.java

Emp_DAO.java

E_Main.java

Here, We are inserting 10 records into the database,

op1

Then we are setting the FirstResult(5), It will print the records from 6. We are also setting MaxResult(3) so it should print the 3 records (Ename6, Ename7, Ename8)

op2

 

 

 

Read More

Criteria Queries Implementation – Hibernate

Criteria Queries Implementation – Hibernate
Hibernate provides org.hibernate.criteria interface to create Criteria Queries.

Consider an example, We have an entity class named “Employee”  then our criteria query will look like,

Criteria criteria = session.createCriteria(Employee.class);

Adding Restrictions:
To use condition-based criteria queries in hibernate application we need to add restrictions to the query. Here let us see some examples of adding restrictions to Hibernate Queries.

Before proceeding to example, let us see some of the standard Restrictions.

.add(Restrictions.eq(Propertyname, object value) – This will compare the property name equals to the object value

.add(Restrictions.gt(Propertyname, object value) – This will return the results when the value of that property is greater than the specified value

.add(Restrictions.lt(Propertyname, object value) – This will return the results when the value of that property is lesser than the specified value

.add(Restrictions.ge(Propertyname, object value) – This will return the results when the value of that property is greater than or equal to the specified value

.add(Restrictions.le(Propertyname, object value) – This will return the results when the value of that property is lesser than or equal to the specified value

.add(Restrictions.like(Propertyname, object value) – This will compare the propertywith specified pattern

.add(Restrictions.between(Propertyname, object low, object high) – This method compares the property within the range of low and high

Now let us see an example,

Structure
structure1

Lets create a table in Database for this example and enter some values,

table

hibernate.cfg.xml

hibernate.reveng.xml

NewHibernateUtil.java

CriteriaExample.java

CriteriaExample.hbm.xml

Controller.java


Output:

This is print the records that matches the city name Berlin

eq

Output 2:

Prints records which has money property value more than 15000

gt

Output 3:

Prints records which has money property value less than or equal to 15000

le

Output 4:

Prints records whose city name property contains “A”

like

 Output 5:

Prints records whose money property value is between 15000 and 22000

between

 

 

Read More

HQL Query Objects in Hibernate

HQL Query Objects in Hibernate
In our previous hibernate article, we have seen CRUD Operations. Similar to SQL Queries, Hibernate uses a Language called HQL – Hibernate Query Language. This is almost similar to SQL, But here instead of Table name we use Entity name and instead of Column name we use Entity properties name.

Once we are through this code, we can understand HQL.

Lets see an example code of using Query Objects in Hibernate. For the purpose of example, I have created a Table with name Student_Table and populated with some values.

Table 1

In this below example, we are going to use Query Object and HQL to list sname from above table.

Structure:
Structure

Student.java

NewHibernateUtil.java

hibernate.cfg.xml

Execution.java

In the above code, we have used “from Student” –> This code is similar to SQL query – Select * from Student_Table
Note that, We have not used table name instead we have used Entity name ‘Student’. In HQL, Instead of Table name we always use Entity name.

Output: 
op1

 

Read More