update record database using hibernate


Update record database using hibernate

Let’s take one simple update record database using hibernate example and understand how to update database record using hibernate framework.

Update record database using hibernate Example

  1. hibernate.cfg.xml
  2. studentinfo.hbm.xml
  3. StudentInfo.java
  4. Run.java

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
 <property name="hbm2ddl.auto">update</property>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
 <property name="connection.username">system</property>
 <property name="connection.password">manager</property>
 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
 <mapping resource="com/javagf/bean/StudentInfo.hbm.xml" />
 </session-factory>
</hibernate-configuration>

studentinfo.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 16 Jun, 2017 7:57:05 PM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
 <class name="com.javagf.bean.StudentInfo" table="STUDENTINFO">
 <id name="roll" type="int">
 <generator class="assigned" />
 </id>
 <property name="name" type="java.lang.String"></property>
 <property name="address" type="java.lang.String"></property>
 </class>
</hibernate-mapping>

StudentInfo.java

package com.javagf.bean;
public class StudentInfo {
 private int roll;
 private String name; 
 private String address;
 public int getRoll() { return roll; }
 public void setRoll(int roll) { this.roll = roll; }
 public String getName() { return name; }
 public void setName(String name) { this.name = name; }
 public String getAddress() { return address; }
 public void setAddress(String address) { this.address = address; }
}

Run.java

package com.javagf.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.javagf.bean.StudentInfo;
public class Run {
 public static void main(String[] args) {
 Configuration cfg = new Configuration(); 
 cfg.configure("com/javagf/hibernate/hibernate.cfg.xml");
 SessionFactory factory = cfg.buildSessionFactory();
 Session session = factory.openSession();
 Transaction t = session.beginTransaction();
 StudentInfo studentInfo = new StudentInfo();
 studentInfo.setRoll(101);// where clause record will be updated on roll basis
 studentInfo.setName("Vikas Kumar"); 
 studentInfo.setAddress("Pune"); 
 session.update(studentInfo);
 t.commit(); session.close();
 System.out.println("Record updated.."); }
}

Output

Record updated..

Related Posts

  1. Why hibernate is better than JDBC
  2. Hibernate Architecture
  3. Steps to create hibernate application in eclipse
  4. Hibernate Mapping and Configuration file
  5. Hibernate SQL Dialects List
  6. Hibernate Query Language
  7. Hibernate ID generator

 

 






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229