Hibernate Delete Data Example
In this section, you will learn to how to delete data from database through hibernate.
DeleteHibernateExample.java:
package developerhelpway.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class DeleteHibernateExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Session sess = null;
Transaction tran = null;
try{
SessionFactory sessFact = new Configuration().
configure().buildSessionFactory();
sess = sessFact.openSession();
tran = sess.beginTransaction();
Employee emp = (Employee) sess.load(Employee.class
, 2);
sess.delete(emp);
tran.commit();
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
sess.close();
}
}
} |
Download: DeleteHibernateExample.java
Output:
