<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: blue;">In this chapter we will learn about how to use configuration file of the model class in stead of Annotation as used in the previous chapter and also learn about how to define model class property in the XML configuration fine or object mapping file(.hbm.xml).</div>
<div style="color: blue;">
<div align='center' id="ads-id"></div>
</div>
<div style="color: blue;">As previous chapter we have write the model class <b>UserDetalis</b>.<b>java</b> </div>
<div style="background-color: #ff99cc; border-width: 1px;">
<div style="background-color: #ff99cc;"><b>package com.sdnext.hibernate.tutorial.dto;</b><b> ;</b><br />
<b>public class UserDetails <br />
{<br />
 ; ; ; private int ; ; ; userId;<br />
 ; ; ; private String userName;<br />
 ; ; ; public int getUserId() {<br />
 ; ; ;  ; ; ; return userId;<br />
 ; ; ; }<br />
 ; ; ; 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 />
}</b></div>
</div>
<p><b>Mapping the UserDetalis Object to the Database UserDetails table</b><br />
The file <b>userdetails.hbm.xml</b> is used to map userDetail Object to the UserDetails table in the database. Here is the code for <b>userdetails.hbm.xml</b>:</p>
<div style="background-color: #ff99cc; border-width: 1px;"><b><;?xml version=&#8221;1.0&#8243;?>;<br />
<;!DOCTYPE hibernate-mapping PUBLIC<br />
&#8220;-//Hibernate/Hibernate Mapping DTD 3.0//EN&#8221;<br />
&#8220;http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd&#8221;>;</p>
<p><;hibernate-mapping>;<br />
 ; <;class name=&#8221;</b><b>com.sdnext.hibernate.tutorial.dto.UserDetails&#8221; table=&#8221;UserDetails&#8221;>;<br />
 ; ; ; ; ; ; <;id name=&#8221;userId&#8221; type=&#8221;long&#8221; column=&#8221;ID&#8221; >;<br />
 ; ; ; ; ; ; ; ; ; ; ; <;generator class=&#8221;assigned&#8221;/>;<br />
 ;  ; ; <;/id>;</p>
<p> ; ; ; <;property name=&#8221;userName&#8221;>;<br />
 ; ; ; ; ; ; ; ; <;column name=&#8221;UserName&#8221; />;<br />
 ; ; ; <;/property>;<br />
 ; ; ; <;/class>;<br />
<;/hibernate-mapping>;</b></div>
<p><b>Configuring Hibernate:</b> here some change is required in the configuration file because we are used mapping file in stead of Annotation.<br />
<b>hibernate.cfg.xml:</b><br />
<b></b></p>
<div style="background-color: #ff99cc;"><b><;?xml version=&#8221;1.0&#8243; encoding=&#8221;UTF-8&#8243;?>;<br />
<;!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;>;username<;/property>; <br />
 ; ; ; ;  ; ; ; <;property name=&#8221;connection.password&#8221;>;password<;/property>; <br />
 ; ; ; ; ; ; ; ; <;property name=&#8221;connection.pool_size&#8221;>;1<;/property>; <br />
 ; ; ;  ; ; ; ; <;!&#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>; <br />
 ; ; ;  ; ; ; <br />
 ; ; ;  ; ; ; <;!&#8211; Disable the second-level cache &#8211;>;<br />
 ; ; ; ;  ; ; ; <;property name=&#8221;cache.provider_class&#8221;>;org.hibernate.cache.NoCacheProvider<;/property>; </p>
<p> ; ; ;  ; ; ; <;!&#8211; Show all executed SQL to stdout &#8211;>;<br />
 ; ; ; ;  ; ; ; <;property name=&#8221;show_sql&#8221;>;true<;/property>; <br />
 ; ; ;  ; ; ; <br />
 ; ; ;  ; ; ; <;!&#8211; Drop and re-create the database schema on startup &#8211;>;<br />
 ; ; ; ;  ; ; ; <;property name=&#8221;hbm2ddl.auto&#8221;>;create<;/property>; <br />
 ; ; ; ;  ; ; ; <br />
 ; ; ; ;  ; ; ; </b><b><;!&#8211; Mapping files &#8211;>;<br />
 ; ; ; ; ; ; <;mapping resource=&#8221;</b><b>userdetails.hbm.xml</b>&#8220;/>;<br />
<b> ; ; ; ;  ; ; ;  ; ; ; ;  ; ; ; <br />
 ; ; ; ; <;/session-factory>; <br />
 ;<;/hibernate-configuration>;</b></div>
<div style="color: blue;">
Here we are used <b>mapping resource</b></div>
<p> ;<b> <;mapping resource=&#8221;</b><b>userdetails.hbm.xml</b>&#8220;/>;</p>
<div style="color: blue;"> ;in stead of <b>mapping class</b>.</div>
<p><b> ; ; <;mapping class=&#8221;com.sdnext.hibernate.tutorial.dto.UserDetails&#8221;/>;</b></p>
<p><b>Developing Code to Test Hibernate example:</b><br />
Now there are three steps for using Hibernate API<b> ;</b></p>
<ul>
<li><b>Create the session factory</b></li>
<li><b>Create the session object from the session factory</b></li>
<li><b>Saving the model object using session object</b></li>
</ul>
<p><b>HibernateTestDemo.java</b><br />
<b></b></p>
<div style="background-color: #ff99cc;">package com.sdnext.hibernate.tutorial;</p>
<p>import java.util.Date;</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;<br />
public class HibernateTestDemo {<br />
 ; ; ; public static void main(String[] args) <br />
 ; ; ; {<br />
<b> ; ; ;  ; ; //Create the model object </b><br />
 ; ; ; ; ; ; UserDetails user = new UserDetails();<br />
 ; ; ;  ; ; ; user.setUserId(1);<br />
 ; ; ;  ; ; ; user.setUserName(&#8220;Dinesh Rajput&#8221;);<br />
 ; ; ; <br />
<b> ;  ;  ; ; // Create Session Factory Object ; &#8211; using configuration object</b><br />
 ; ; ;  ; ; ; SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); ; <br />
 ; ; ; <br />
 ;  ;<b>  ; //Create Session object from session factory object</b><br />
 ;  ;  ;  ; Session session = sessionFactory.openSession();<br />
 ; ; ;  ; ; ; session.beginTransaction();<br />
 ; ; ; <br />
 ;  ; ;  ; <b style="color: blue;">//Use the session to save model objects</b><br />
 ; ; ; ; ; ; ; session.save(user);<br />
 ; ; ;  ; ; ; session.getTransaction().commit();<br />
 ; ; ;  ; ; ; session.close();<br />
 ; ; ;  ; ; }<br />
}</div>
<p>Now you can run the application and see the result.<br />
So in this topic we see how to use Mapping file to map the Model Object to relational database table.</p>
<p>
<b>In the Next Chapter We will Described the O/R Mapping file in details.</b><br />
<b><br />
</b> <b><br />
</b> <b> ;  ;  ;  ;  ;  ;  ;  ;  ;  ; ;<;<;<a href="https://dineshonjava.com/p/chapter-4-writing-first-hibernate.html">Previous Chapter 4</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/chapter-6-understanding-hibernate-or.html">ext Chapter6</a>>;>;</b></p>
</div>