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-
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-
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 {
student.setName("Johny");
student.setCourse("Hibernate");
session.merge(student);
transaction.commit();
System.out.println("Update Successfully");
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Hibernate Exception
Post your Comment