<div dir="ltr" style="text-align: justify;" trbidi="on">In this tutorial we will show how to update a row with new information by retrieving data from the underlying database using the hibernate. Lets first write a java class to update a row to the database.<br />
<b>Create a java class:</b></p>
<p>here we update the value of the field name <b>userName </b>with corresponding column name is <b>USER_NAME</b> in the database table <b>USRE_TABLE</b>.</p>
<div align='center' id="ads-id"></div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table3.jpg"/></div>
<p>In the above table we want to update the value of the<b> USER_NAME</b> column of the<b> USER_TABLE</b> table associated <b>user </b>model object which <b>userId = 2</b><br />
current value of the <b>userName </b>is<b> &#8216;Anamika Rajput&#8217;</b> update to <b>userName=&#8217;Sweety&#8217;.</b><br />
Look the following class file.<br />
<b>HibernateTestDemo.java</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">package com.sdnext.hibernate.tutorial;</p>
<p>import org.hibernate.Session;<br />
import org.hibernate.SessionFactory;<br />
import org.hibernate.cfg.AnnotationConfiguration;</p>
<p>import com.sdnext.hibernate.tutorial.dto.UserDetails;</p>
<p>public class HibernateTestDemo {<br />
 ; ; ; /**<br />
 ; ; ;  ;* @param args<br />
 ; ; ;  ;*/<br />
 ; ; ; public static void main(String[] args) <br />
 ; ; ; {<br />
 ; ; ;  ; ; ; UserDetails user = null;<br />
 ; ; ; <br />
 ;SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();<br />
 ; ; ;  ; ; ; Session session = sessionFactory.openSession();<br />
 ; ; ;  ; ; ; session.beginTransaction();<br />
 ; ; ;  ; ; ; <br />
 ; ; ;  ; ; ; user = (UserDetails) session.get(UserDetails.class, 2); //Retrieving object which we want to update<br />
 ; ; ;  ; ; ; user.setUserName(&#8220;Sweety&#8221;); //Set the updated userName to the model field<br />
 ; ; ;  ; ; ; session.update(user); //Update to the database table<br />
 ; ; ;  ; ; ; session.getTransaction().commit();</p>
<p> ; ; ;  ; ; ; System.out.println(&#8220;Updated User ->;&#8221;+user);<br />
 ; ; ; <br />
 ;  ;  ; session.close();<br />
 ; ; ; }<br />
}</p>
<p>Output:<br />
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).<br />
log4j:WARN Please initialize the log4j system properly.<br />
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.USER_NAME as USER2_0_0_ from USER_TABLE userdetail0_ where userdetail0_.USER_ID=?<br />
Hibernate: update USER_TABLE set USER_NAME=? where USER_ID=?<br />
<b>[Updated User ->; User Name: Sweety User Id: 2]</b> </div>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="76" src="https://dineshonjava.com/wp-content/uploads/2017/04/output3-1024x122.jpg" width="640" /></div>
<p>Now we get the following updated table&#8230;</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table4.jpg"/></div>
<blockquote class="tr_bq">
<blockquote class="tr_bq"><p>We are looking the value of the <b>USER_NAME</b> column updated from &#8216;<b>Anamika Rajput</b>&#8216; to &#8216;<b>Sweety</b>&#8216; in the <b>USER_TABLE</b> table.</p></blockquote>
</blockquote>
<p>
<b>Now in the <a href="https://dineshonjava.com/p/hibernate-delete-query.html">Next Chapter </a>we will see how to <a href="https://dineshonjava.com/p/hibernate-delete-query.html">delete a row in the hibernate</a>.</b><br />
<b><br />
</b> <b><br />
</b>  ; ; ; ; ;  ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ; ; <;<;<a href="https://dineshonjava.com/p/retrieving-objects-using-sessionget.html">Previous Chapter 10</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/hibernate-delete-query.html">ext Chapter12</a></b></p>
</div>

View Comments
how to fetch data from multiple tables using createcriteria in CRUD operations???
We no need to call updated() method explicitly here just following line code is enough to update the user entity
user = (UserDetails) session.get(UserDetails.class, 2); user.setUserName(“Sweety”);
session.getTransaction().commit();
change the color ...
change the color code...