<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">Here we will learn about hbm2ddl Configuration in the hibernate configuration file (hibernate.cfg.xml). Actually hbm2ddl Configuration means hibernate mapping to create schema DDL (Data Definition Language).</p>
<div align='center' id="ads-id"></div>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"> ; ; ; ; ; ; ; ; ; <;!&#8211; Drop and re-create the database schema on startup &#8211;>;<br />
 ; ; ; ;  ; ; ; <;property name=&#8221;hbm2ddl.auto&#8221;>;create<;/property>; ;</div>
<p>
Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.<br />
 ; </p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="background-color: #ff99cc;"><;?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 />
 ; ; ; ;  ; ; ; <;!&#8211; Mapping files &#8211;>;<br />
 ; ; ; ; ; ; <;mapping resource=&#8221;userdetails.hbm.xml&#8221;/>;<br />
 ; ; ; ;  ; ; ;  ; ; ; ;  ; ; ; <br />
 ; ; ; ; <;/session-factory>; <br />
 ;<;/hibernate-configuration>;</div>
</div>
<p>
Here <b><;property name=&#8221;hbm2ddl.auto&#8221;>;create<;/property>;</b> means schema DDL created every time when SessionFactory Object is created.</p>
<p><b>UserDetails.java</b> </p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">package com.sdnext.hibernate.tutorial.dto;<br />
import javax.persistence.Entity;<br />
import javax.persistence.Column;<br />
import javax.persistence.Id;<br />
import javax.persistence.Table;<br />
@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;<br />
 ; ; @Column(name=&#8221;USER_NAME&#8221;)<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> </b></div>
<p>
Now we run this example&#8211;</p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">public class HibernateTestDemo {<br />
 ; ; ; public static void main(String[] args) <br />
 ; ; ; {<br />
<b> ; ; ;  ; ; //Create the model object </b><br />
 ; ; ; ; ; ; UserDetails user1 = new UserDetails();<br />
 ; ; ; ; ; ; UserDetails user2 = new UserDetails();<br />
 ; ; ;  ; ; ; user1.setUserId(1);<br />
 ; ; ;  ; ; ; user1.setUserName(&#8220;Dinesh Rajput&#8221;);<br />
 ; ; ; ; ; ; ; user2.setUserId(2);<br />
 ; ; ;  ; ; ; user2.setUserName(&#8220;Anamika Rajput&#8221;);  ; ; ; <br />
<b> ;  ;  ; ; // Create Session Factory Object ; &#8211; using annotation configuration object</b><br />
 ; ; ;  ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().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(user1);<br />
 ; ; ; ; ; ; ; session.save(user2);<br />
 ; ; ;  ; ; ; session.getTransaction().commit();<br />
 ; ; ;  ; ; ; session.close();<br />
 ; ; ;  ; ; }<br />
}</p>
<div style="color: red;"><b>Run this example&#8211;</b></div>
<p>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_DETAILS ( USER_ID, USER_NAME) values (?, ?)<br />
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)</div>
<p>
Now we get following table structure&#8211;</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table.jpg" /></div>
<p>
Again we running the following example-</p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">public class HibernateTestDemo {<br />
 ; ; ; public static void main(String[] args) <br />
 ; ; ; {<br />
<b> ; ; ;  ; ; //Create the model object </b><br />
 ; ; ; ; ; ; UserDetails user1 = new UserDetails();<br />
 ; ; ; ; ; ; UserDetails user2 = new UserDetails();<br />
 ; ; ;  ; ; ; user1.setUserId(3);<br />
 ; ; ;  ; ; ; user1.setUserName(&#8220;Dev Rajput&#8221;);<br />
 ; ; ; ; ; ; ; user2.setUserId(4);<br />
 ; ; ;  ; ; ; user2.setUserName(&#8220;Sweety Rajput&#8221;);  ; ; ; <br />
<b> ;  ;  ; ; // Create Session Factory Object ; &#8211; using annotation configuration object</b><br />
 ; ; ;  ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().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(user1);<br />
 ; ; ; ; ; ; ; session.save(user2);<br />
 ; ; ;  ; ; ; session.getTransaction().commit();<br />
 ; ; ;  ; ; ; session.close();<br />
 ; ; ;  ; ; }<br />
}</p>
<div style="color: red;"><b>Run this example&#8211;</b></div>
<p>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_DETAILS ( USER_ID, USER_NAME) values (?, ?)<br />
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?) </div>
<p>
Now we get the following table schema&#8211;</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table2.jpg" /></div>
<p>
Here we see that the previous data is destroyed when we are using in the <b>hibernate.cfg.xml </b>file using the following line.<br />
<b> ; ; ; ;  ; ; ; <;property name=&#8221;hbm2ddl.auto&#8221;>;create<;/property>; ;</b><br />
<b>Here </b><br />
hbm2ddl.auto&#8211;>;create -Always create new schema <br />
hbm2ddl.auto&#8211;>;update -Update existing schema<br />
Now we are replacing above line with the ; <b> <;property name=&#8221;hbm2ddl.auto&#8221;>;update<;/property>;</b></p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="background-color: #ff99cc; border-width: thin; border: solid;">public class HibernateTestDemo {<br />
 ; ; ; public static void main(String[] args) <br />
 ; ; ; {<br />
<b> ; ; ;  ; ; //Create the model object </b><br />
 ; ; ; ; ; ; UserDetails user1 = new UserDetails();<br />
 ; ; ; ; ; ; UserDetails user2 = new UserDetails();<br />
 ; ; ;  ; ; ; user1.setUserId(1);<br />
 ; ; ;  ; ; ; user1.setUserName(&#8220;Dinesh Rajput&#8221;);<br />
 ; ; ; ; ; ; ; user2.setUserId(2);<br />
 ; ; ;  ; ; ; user2.setUserName(&#8220;Anamika Rajput&#8221;);  ; ; ; <br />
<b> ;  ;  ; ; // Create Session Factory Object ; &#8211; using annotation configuration object</b><br />
 ; ; ;  ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().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(user1);<br />
 ; ; ; ; ; ; ; session.save(user2);<br />
 ; ; ;  ; ; ; session.getTransaction().commit();<br />
 ; ; ;  ; ; ; session.close();<br />
 ; ; ;  ; ; }<br />
}</p>
<div style="color: red;"><b>Run this example&#8211;</b></div>
<p>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_DETAILS ( USER_ID, USER_NAME) values (?, ?)<br />
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)</div>
</div>
<p>
Now we get the following table schema&#8230;</p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://dineshonjava.com/wp-content/uploads/2017/04/table3.png" /></div>
<p>here we are looking the table USER_DETAILS only updated not again created.</p>
<p><b>Now in the <a href="https://dineshonjava.com/p/retrieving-objects-using-sessionget.html">Next Chapter</a> we will learn about <a href="https://dineshonjava.com/p/retrieving-objects-using-sessionget.html">how to retrieve an object from database.</a></b></p>
<p>  ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<b> ;</b><b><;<;<a href="https://dineshonjava.com/p/using-annotations-vs-configuration.html">Previous Chapter 8</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/retrieving-objects-using-sessionget.html">ext Chapter10</a>>;>;</b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b></div>
<p>
</div>