Approach 2:

 Create the POJO class object and supply the primary key value. Mark the POJO class object as to be deleting by calling the delete method.

Ex:

public class DeleteRecords1{

public static void main(String[] args){
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session hsession = sf.openSession();
Product p = new Product();
p.setpid(11);
hsession.delete(p);
tx.commit();
hsession.close();
           }
}

In this approach when we call the delete method object is marked as to be deleted. When we call the commit() method it has perform the following 3 steps.

Step 1: It check weather primary key value is available in the supplied object or not. If not available it will not carry out any work.
Step 2: If the primary key value is available a select query will be send to database server to check weather record is available or not.
Step 3: If the record is available in DB hibernate send the delete query. If the record is not available hibernate will not do any work.

0 comments:

Post a Comment