
How to update value of database using hibernate?

Hibernate provides facility to update the existing record. Here is an example
Example-
package net.roseindia.main;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class HibernateUpdate{
public static void main(String [] args){
SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
Session session=sessionFactory.openSession();
Transaction transaction=session.beginTransaction();
Employee employee=(Employee) session.get(Employee.class, new Integer(1));
employee.setName("Mandy");
employee.setSalary(20000);
session.update(employee);
transaction.commit();
session.close();
System.out.println("Updated");
}
}
Output:
Hibernate: select employee0_.emp_id as emp1_0_0_, employee0_.date_of_join as date2_0_0_, employee0_.name as name0_0_, employee0_.salary as salary0_0_ from employee employee0_ where employee0_.emp_id=? Updated
Description: this example updates the record of employee whose id is 1.We can do this by setting the value and calling the update method.
session.update(employee);