Hi Below is my code to update a table:
Code:
Code:
@SuppressWarnings("unchecked")
@Override
public boolean updateData(Object objData) {
if (objData instanceof SchoolProfile) {
this.schoolProfile = (SchoolProfile) objData;
Session session = HibernateUtil.geSessionFactory().openSession();
Transaction tx = session.beginTransaction();
List SchoolProfileList = session.createCriteria(SchoolProfile.class).add(
Restrictions.eq("id", this.schoolProfile.getId())).list();
if (SchoolProfileList.size() == 0) {
System.out.println("Id Could not be Found...");
return false;
}
SchoolProfile schprf = (SchoolProfile) SchoolProfileList.get(0);
schprf.setName(this.schoolProfile.getName());
schprf.setDescription(this.schoolProfile.getDescription());
schprf.setRegno(this.schoolProfile.getRegno());
schprf.setAcstmon(this.schoolProfile.getAcstmon());
schprf.setAddress(this.schoolProfile.getAddress());
schprf.setEmail(this.schoolProfile.getEmail());
schprf.setWebsite(this.schoolProfile.getWebsite());
session.update(schprf);
tx.commit();
session.flush();
session.close();
return true;
}
return false;
}
In this
IDis the primary key I get it from database and then I update with the values. But some times My update from Controller updates only some column like say Website. Here in this to update only that I have to execute 'set' methods for all the attributes.
My question is - Do I have to retrieve the data from database to update it. Can't I do it directly without retrieving. If yes, Would you please tell how?