Home Answers Viewqa Hibernate Use of delete() method in hibernate.

 
 


ratna rathor
Use of delete() method in hibernate.
1 Answer(s)      11 months ago
Posted in : Hibernate

Define the use of delete() method of hibernate?

View Answers

June 11, 2012 at 5:34 PM


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);









Related Pages:

Ask Questions?

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.