<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="color: blue;">Lets we start on this topics. First we will see what is the process of using database without using Hibernate.</div>
<div style="color: blue;">
</div>
<div style="color: blue;"><b>Saving Without Hibernate ->;</b></div>
<ul style="color: blue;">
<li>JDBC Database configuration</li>
<li>The Model Object</li>
<li>Service method to create the model object</li>
<li>Database Design ;</li>
<li>DAO method to use saving the model object to the database using SQL queries.</li>
</ul>
<div align='center' id="ads-id"></div>
<div style="color: blue;"><b>With Hibernate Way->;</b> </div>
<ul style="color: blue;">
<li>JDBC Database Configuration ; <b style="color: purple;">->; ; Hibernate Configuration</b></li>
<li>The Model Object ;<b style="color: purple;"> ; ->; Using Annotation</b></li>
<li>Service method to create the model object <b style="color: purple;">->; Using the Hibernate API</b></li>
<li>Database Design ; ;<b style="color: purple;"> ->; Not Needed</b></li>
<li>DAO method to use saving the model object to the database using SQL queries<b style="color: purple;"> ->; Not needed</b></li>
</ul>
<div style="color: blue;">Now as above steps of using hibernate, firstly we have to create <b>Hibernate Configuration file.</b></div>
<p><b style="color: blue;">Step 1: File Name ->; hibernate.cfg.xml </b>in<b style="color: blue;"> src </b>folder of the application<b> </b></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/hibernate.jpg"/></div>
<p><b><br />
</b></p>
<div style="color: blue;">Now we need to type all the detail related to MySQL database so we can use in our application.</div>
<div style="color: blue;"><b>hibernate.cfg.xml</b> </div>
<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 />
 ; ; ; ;  ; ; ; ; <;mapping class=&#8221;com.sdnext.hibernate.tutorial.dto.UserDetails&#8221;/>;<br />
 ; ; ; ;  ; ; ;  ; ; ; ;  ; ; ; <br />
 ; ; ; ; <;/session-factory>; <br />
 ;<;/hibernate-configuration>;</b></div>
<div style="color: blue;">
</div>
<p>Here<b style="color: blue;"> </b>below property configure ; driver of the specific database<b><br />
</b><br />
<b><;property name=&#8221;connection.driver_class&#8221;>;com.mysql.jdbc.Driver<;/property>;</b></p>
<div style="color: blue;">Next line we configure the Database <b>connection url</b></div>
<div style="color: blue;">suppose we want to connect the database name<b> hibernateDB ;</b></div>
<p><b> <;property name=&#8221;connection.url&#8221;>;jdbc:mysql://localhost:3306/hibernateDB<;/property>;</b></p>
<div style="color: blue;">Next two lines set the user name and password of the connecting database <b>hibernateDB</b></div>
<p><b> ;<;property name=&#8221;connection.username&#8221;>;username<;/property>; <br />
 ;<;property name=&#8221;connection.password&#8221;>;password<;/property>; ;</b></p>
<div style="color: blue;">Next line configure Dialect of the database<b> MySQL, every database has its own Dialect.</b></div>
<div style="color: blue;"><b>What is </b><b>Dialect? </b>means Dialect is configuration specify here so hibernate knows whats kind of language we are used and what type database we are used. we can say it is database dependent. It connects the database specific query language which we want to use. </div>
<p><b><;property name=&#8221;dialect&#8221;>;org.hibernate.dialect.MySQLDialect<;/property>; ;</b><br />
<b> ; ;</b><br />
The below line configured the <b style="color: blue;">model class</b> name <b style="color: blue;">UserDetails</b> its object we want save on the database<b> hibernateDB</b><br />
<b><;mapping class=&#8221;com.sdnext.hibernate.tutorial.dto.UserDetails&#8221;/>;</b></p>
<div style="color: blue;">Now step 1 is over now we move to another step Now we have to create a Model class<b> ; ;</b></div>
<p><b>Step 2: UserDetails.java</b></p>
<div style="background-color: #ff99cc;"><b>package com.sdnext.hibernate.tutorial.dto;<br />
import java.util.Date;<br />
 ;</b><br />
<b>import javax.persistence.Entity;<br />
import javax.persistence.Id;<br />
 ;</b><br />
<b>@Entity<br />
public class UserDetails <br />
{<br />
 ; ; ; @Id<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 style="color: blue;">Here <b>@Entity</b> means it telling hibernate this class treat as entity and need to save it the database.</div>
<div style="color: blue;">and <b>@ID </b>means it telling hibernate this property treat as primary key of the table.</div>
<div style="color: blue;">these are two minimum required annotation we have use for saving the object in the database.</div>
<p></p>
<div style="color: blue;">Now we move to next step create service method to save the model object in the database.</div>
<div style="color: blue;"><i><b>Using the Hibernate API</b></i></div>
<ul style="color: blue;">
<li><b>Create a session factory</b></li>
<li><b>create a session from the session factory</b></li>
<li><b>Use the session to save model objects </b></li>
</ul>
<div style="color: blue;"><b>Step 3: HibernateTestDemo.java</b></div>
<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 style="background-color: white; color: blue;">//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 using Annotation Configuration</b><br />
 ; ; ;  ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); ; <br />
 ; ; ; <br />
 ;  ;  ;<b style="color: blue;"> //Create Session object from session factory object</b><br />
 ;  ;  ;  ; Session session = sessionFactory.openSession();<br />
 ; ; ;  ; ; ; session.beginTransaction();<br />
 ; ; ; <br />
 ;  ; ; <b> ; //Use the session to save model objects</b><br />
 ; ; ; ; ; ; ; session.save(user);<br />
 ; ; ;  ; ; ; session.getTransaction().commit();<br />
 ; ; ;  ; ; ; session.close();<br />
 ; ; ;  ; ; }<br />
}</div>
<div style="color: blue;">Now run application now we get a <b>Table </b>with name <b>UserDetails </b>as its <b>model class</b> name with two <b>columns </b>name are <b>ID </b>and <b>UserName </b>as class <b>property </b>name.</div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/vlcsnap-2012-03-31-00h01m00s148.png"/></div>
<p><b><a href="https://dineshonjava.com/p/chapter-5-writing-first-hibernate.html">Next Chapter we will write hibernate application using mapping file for the model class </a></b></p>
<p>
 ;  ;  ;  ;  ;  ;  ;  ;  ; <b> ; ;<;<;<a href="https://dineshonjava.com/p/chapter-3-setting-up-hibernate-to.html">Previous Chapter 3</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/chapter-5-writing-first-hibernate.html">ext Chapter5</a>>;>;</b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b></div>

View Comments
Can i need to create a table in DB already or not?
Hibernate Automatically creates for you... u dont need to
here while creating session factory we can not pass "hibernate.cnf.xml " in configure() method....it's possible or we don't need it
table will automatically created with the model class name if not created already.
Hi Dinesh, the pink colour on the coding part seems dull-looking not readable. You may change the respective colour if you wish :)