Hibernate Update Record

Hibernate Update Record

In hibernate applications, saveOrUpdate() method can be used to update a record into the Database table.

Employee.java

package com.ashok.hibernate.update.model;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class Employee {
	private String empId;
	private String empName;
	private String address;
	private double salary;
	
	public Employee() {
		super();
	}
	
	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}
	
	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}
}

employee.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.ashok.hibernate.update.model.Employee" table="emp">
		<id name="empId" column="emp_id" type="string" />
		<property name="empName" column="emp_name" type="string" />
		<property name="address" column="address" type="string" />
		<property name="salary" column="salary" type="double" />
	</class>
</hibernate-mapping>

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
      <property name="hibernate.connection.username">system</property>
      <property name="hibernate.connection.password">ashok</property>

      <mapping resource="com/ashok/hibernate/update/employee.hbm.xml"/>
   </session-factory>
</hibernate-configuration>

ClientApp.java

package com.ashok.hibernate.update;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.ashok.hibernate.update.model.Employee;

/**
 * 
 * @author ashok.mariyala
 *
 */
public class ClientApp {
	public static void main(String[] args) throws Exception {
		SessionFactory sessionFactory = null;
		Session session = null;
		try {
			Configuration cfg = new Configuration();
			cfg.configure("/com/ashok/hibernate/update/hibernate.cfg.xml");
			sessionFactory = cfg.buildSessionFactory();
			session = sessionFactory.openSession();
			Transaction tx = session.beginTransaction();
			Employee emp = new Employee();
			emp.setEmpId("E0087");
			emp.setEmpName("Ashok Kumar");
			emp.setSalary(79000);
			emp.setAddress("Hyderabad");
			session.saveOrUpdate(emp);
			tx.commit();
			System.out.println("Employee Record updated Succesfully");
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != session) {
				session.close();
			}
			if(null != sessionFactory) {
				sessionFactory.close();
			}
		}
	}
}

Output

Employee Record updated Succesfully
Hibernate Update Record
Scroll to top