Define the use of delete() method of hibernate?
Hibernate provide the facility to delete one row or multiple row from a table.
Here is an example.
Example:
package net.roseindia.main;
import net.roseindia.table.Department;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateDelete{
public static void main(String []args){
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
int deptId=2;
Object object=session.load(Department.class,deptId);
session.delete(object);
transaction.commit();
}
}
Output:
Hibernate: select department0_.id as id2_0_, department0_.dept_name as dept2_2_0_ from department department0_ where department0_.id=? Hibernate: delete from department where id=? Department record of Id=2 is deleted successfully
Description: This program deletes the row whose deptId
is 2.Create an instance of Object as
Object object=session.load(Department.class,deptId);
Here passing our Table class Department and the deptId. Now you can delete record by calling delete method as session.delete(object);