Approach 1

Updating a record into database server which ever the record we would like to update load the record by calling load() method modify the values by using setter methods in the loaded POJO class object. Now mark the object as to be updated.

Ex:

public class UpdateRecord{
public static void main(String[] args){
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session hsession = sf.openSession();
Emp e = new Emp();
hsession.load(e, new BigDecimal(2));
e.setName(“Raju”);
hsession.update(e);
tx.commit();
hsession.close();
           }
}


Approach 2: Hibernate uses a directory object technique to check weather object value is modified or not. If the value is not modified. Hibernate will not send any update query to the database server. If the values are modified Hibernate send an update query to database server.


Approach 3: In this approach create POJO class object to the class which we would like to update the record and store the data into POJO class object. We need to mark the object as to be updated.

Ex

public class UpdateRecord{
public static void main(String[] args){
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session hsession = sf.openSession();
Emp e = new Emp();
e.setEno(new BigDecimal(20));
e.setName(“ttt”);
hsession.update(e);
tx.commit();
hsession.close();
        }
}

0 comments:

Post a Comment