<div dir="ltr" style="text-align: justify;">
<p>We&#8217;ll look at one of the several ways we can fetch data from the database using Hibernate: the <b>session.get</b> method.</p>
<p>Now look following example&#8230;</p>
<div id="ads-id" align="center"></div>
<p><b>UserDetails.java</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p>package com.sdnext.hibernate.tutorial.dto;</p>
<p>import javax.persistence.Column;<br />
import javax.persistence.Entity;<br />
import javax.persistence.Id;<br />
import javax.persistence.Table;</p>
<p>@Entity<br />
@Table (name=&#8221;USER_TABLE&#8221;)<br />
public class UserDetails<br />
{<br />
@Id<br />
@Column(name=&#8221;USER_ID&#8221;)<br />
private int userId;</p>
<p>@Column(name=&#8221;USER_NAME&#8221;)<br />
private String userName;</p>
<p>public void setUserId(int userId) {<br />
this.userId = userId;<br />
}<br />
public String getUserName() {<br />
return userName;<br />
}<br />
public void setUserName(String userName) {<br />
this.userName = userName;<br />
}<br />
public String toString()<br />
{<br />
return &#8220;[User Name: &#8220;+userName+&#8221;User Id: &#8220;+userId+&#8221;]&#8221;;<br />
}<br />
}</p>
</div>
<p>This is user domain persistence class <b>UserDetails</b> to store data into table <b>USER_TABLE</b> with columns name USER_ID and USER_NAME corresponding persisting field userId and userName respectively.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>Now we have to configure hibernate configuration file.</p>
<p><b>hibernate.cfg.xml</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p><;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?>;</p>
<p><;!DOCTYPE hibernate-configuration PUBLIC<br />
&#8220;-//Hibernate/Hibernate Configuration DTD 3.0//EN&#8221;<br />
&#8220;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&#8221;>;</p>
<p><;hibernate-configuration>;<br />
<;session-factory>;<br />
<;!&#8211; Database connection settings &#8211;>;<br />
<;property name=&#8221;connection.driver_class&#8221;>;com.mysql.jdbc.Driver<;/property>;<br />
<;property name=&#8221;connection.url&#8221;>;jdbc:mysql://localhost:3306/hibernateDB<;/property>;<br />
<;property name=&#8221;connection.username&#8221;>;root<;/property>;<br />
<;property name=&#8221;connection.password&#8221;>;root<;/property>;</p>
<p><;!&#8211; JDBC connection pool (use the built-in) &#8211;>;<br />
<;property name=&#8221;connection.pool_size&#8221;>;1<;/property>;</p>
<p><;!&#8211; SQL dialect &#8211;>;<br />
<;property name=&#8221;dialect&#8221;>;org.hibernate.dialect.MySQLDialect<;/property>;</p>
<p><;!&#8211; Enable Hibernate&#8217;s automatic session context management &#8211;>;<br />
<;property name=&#8221;current_session_context_class&#8221;>;thread<;/property>;</p>
<p><;!&#8211; Disable the second-level cache &#8211;>;<br />
<;property name=&#8221;cache.provider_class&#8221;>;org.hibernate.cache.NoCacheProvider<;/property>;</p>
<p><;!&#8211; Echo all executed SQL to stdout &#8211;>;<br />
<;property name=&#8221;show_sql&#8221;>;true<;/property>;</p>
<p><;!&#8211; Drop and re-create the database schema on startup &#8211;>;<br />
<;property name=&#8221;hbm2ddl.auto&#8221;>;update<;/property>;</p>
<p><;mapping class=&#8221;com.sdnext.hibernate.tutorial.dto.UserDetails&#8221;/>;</p>
<p><;/session-factory>;<br />
<;/hibernate-configuration>;</p>
</div>
<p>here we are using MySql database <b>hibernateDB. </b>Now we running the code with following class file<br />
<b>HibernateTestDemo.java</b><br />
<b><br />
</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p>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 = new UserDetails(); <b style="color: red;">//Creating first user</b><br />
user.setUserId(1);<br />
user.setUserName(&#8220;Dinesh Rajput&#8221;);</p>
<p>UserDetails user2 = new UserDetails();<b style="color: red;">//Creating second user</b><br />
user2.setUserId(2);<br />
user2.setUserName(&#8220;Anamika Rajput&#8221;);</p>
<p>SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); <b style="color: red;">//Creating a session factory object</b><br />
Session session = sessionFactory.openSession(); <b style="color: red;">//Creating a session object for inserting users object to the database table USER_TABLE</b><br />
session.beginTransaction(); <b style="color: red;">//Open the transaction of session object to do something</b></p>
<p>session.save(user); <b>//Inserting or Saving the first user object</b> session.save(user2); <b style="color: red;"> //Inserting or Saving the second user object</b></p>
<p>session.getTransaction().commit();<b style="color: red;">//Close the transaction of session object after to do something</b><br />
session.close(); <b style="color: red;">//Close the session object performing saving event to database</b></p>
<p>user = null; <b style="color: red;">//Now getting a user object from database table from session object</b><br />
session = sessionFactory.openSession(); <b style="color: red;">//Creating a new session object for fetching user object</b><br />
session.beginTransaction();<b style="color: red;"> //Again Open the transaction of the session object</b></p>
<p>user = (UserDetails) session.get(UserDetails.class, 1); <b style="color: red;">//we get user object from session object using method session.get(Class arg1, Serializable arg2) here arg2 is primary key or id of the fetching object and arg1 is the what the model object we want to retrieve from database.</b></p>
<p>System.out.println(user);<br />
}<br />
}</p>
</div>
<p>Here after running this code we get following output&#8230;</p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">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: insert into USER_TABLE (USER_NAME, USER_ID) values (?, ?)<br />
Hibernate: insert into USER_TABLE (USER_NAME, USER_ID) values (?, ?)<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 />
<b>[User Name: Dinesh Rajput User Id: 1]</b></div>
<div class="separator" style="clear: both; text-align: center;"></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/output2.png" border="0" /></div>
<p>In the <a href="https://dineshonjava.com/p/hibernate-update-query.html">Next Chapter</a> we will see that how to work<a href="https://dineshonjava.com/p/hibernate-update-query.html"> update query in hibernate</a>.</p>
<p><b> <;<;<a href="https://dineshonjava.com/p/hbm2ddl-configuration-and-name.html">Previous Chapter 9</a><;<; >;>;N<a href="https://dineshonjava.com/p/hibernate-update-query.html">ext Chapter11</a>>;>;</b></p>
<p> ;</p>
</div>