updating a field of a object in hibernate

Viewed 33826

I have a object A which maps to table A in DB

class A {
     Integer id;
     String field2,field2;field3 ,... fieldN;
     //lots of other attribute
}

Now i want to write a DAO api that just updates a single field.One approach is that i can first load the object then changes the attribute i need and then use merge api

//start transcation
A a = session.load(A.class, id);
A.setfieldP(newValue)
session.merge(A)
//commit transcation

Now if i use following code

 //start transcation
 A a = new A();
 a.setId(id); //set a id by which object A exists in DB
 A.setfieldP(newValue)
 session.merge(A)
 //commit transaction

Now second approach all fields except id and fieldP are set to null

1)Now is there any other approach?
2)Can i use update instead of merge ?

4 Answers
Related