Share on Google+Share on Google+

Hibernate Exception

In this tutorial we will discuss about hibernate exception.

Hibernate Exception

In this tutorial we will discuss about hibernate exception.

Hibernate Exception Handling  :

An exception is kind of error generated during the execution of a program. It interrupts the normal flow of the program.
There are many reasons of exceptions-

  • Entered invalid data
  • File not found that you want to fetch
  • Some network problem or out of memory run.

Entity Manager handle exception as by calling EntityManager.close() which discard the changes and rollback your database transaction.
In general EntityManager handle Hibernate core exception. some of them-

  • IllegalArgumentException: wrong argument or fail to recognized, or having incorrect format etc.
  • EntityNotFoundException: an entity was expected but do not match the requirement
  • TransactionRequiredException: this operation has to be in a transaction
  • IllegalStateException: wrong way of using entity manager.

Hibernate works with most of unchecked hibernate persistence layer exceptions.
When Hibernate interacting with database throws SQLExceptions. Hibernate provides better handle than the JDBCException.

You can use try and catch block to handle the exceptions. Put the line of code in try block which may cause exception.
and according to that catch exception in to catch block.

Example:  Here is an example ,we are putting the code of updating in to try block and which is handle by HIbernateException in catch block.

package net.roseindia.main;

import net.roseindia.table.Student;
import net.roseindia.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class HibernateUpdate {
public static void main(String args[]) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
int roll = 5;
Student student = (Student) session.load(Student.class, roll);
try {
System.out.println("Update Successfully");
} catch (HibernateException e) {




Posted on: July 25, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Exception  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image