<property name=”hbm2ddl.auto”>create</property>
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.
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
“http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
<hibernate-configuration>
<session-factory>
<!– Database connection settings –>
<property name=”connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”connection.url”>jdbc:mysql://localhost:3306/hibernateDB</property>
<property name=”connection.username”>username</property>
<property name=”connection.password”>password</property>
<property name=”connection.pool_size”>1</property>
<!– SQL dialect –>
<property name=”dialect”>org.hibernate.dialect.MySQLDialect</property>
<!– Enable Hibernate’s automatic session context management –>
<property name=”current_session_context_class”>thread</property>
<!– Disable the second-level cache –>
<property name=”cache.provider_class”>org.hibernate.cache.NoCacheProvider</property>
<!– Show all executed SQL to stdout –>
<property name=”show_sql”>true</property>
<!– Drop and re-create the database schema on startup –>
<property name=”hbm2ddl.auto”>create</property>
<!– Mapping files –>
<mapping resource=”userdetails.hbm.xml”/>
</session-factory>
</hibernate-configuration>
Here <property name=”hbm2ddl.auto”>create</property> means schema DDL created every time when SessionFactory Object is created.
UserDetails.java
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name=”USER_TABLE”)
public class UserDetails
{
@Id
@Column(name=”USER_ID”)
private int userId;
@Column(name=”USER_NAME”)
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Now we run this example–
public static void main(String[] args)
{
//Create the model object
UserDetails user1 = new UserDetails();
UserDetails user2 = new UserDetails();
user1.setUserId(1);
user1.setUserName(“Dinesh Rajput”);
user2.setUserId(2);
user2.setUserName(“Anamika Rajput”);
// Create Session Factory Object – using annotation configuration object
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//Create Session object from session factory object
Session session = sessionFactory.openSession();
session.beginTransaction();
//Use the session to save model objects
session.save(user1);
session.save(user2);
session.getTransaction().commit();
session.close();
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Now we get following table structure–

Again we running the following example-
public static void main(String[] args)
{
//Create the model object
UserDetails user1 = new UserDetails();
UserDetails user2 = new UserDetails();
user1.setUserId(3);
user1.setUserName(“Dev Rajput”);
user2.setUserId(4);
user2.setUserName(“Sweety Rajput”);
// Create Session Factory Object – using annotation configuration object
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//Create Session object from session factory object
Session session = sessionFactory.openSession();
session.beginTransaction();
//Use the session to save model objects
session.save(user1);
session.save(user2);
session.getTransaction().commit();
session.close();
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Now we get the following table schema–

Here we see that the previous data is destroyed when we are using in the hibernate.cfg.xml file using the following line.
<property name=”hbm2ddl.auto”>create</property>
Here
hbm2ddl.auto–>create -Always create new schema
hbm2ddl.auto–>update -Update existing schema
Now we are replacing above line with the <property name=”hbm2ddl.auto”>update</property>
public static void main(String[] args)
{
//Create the model object
UserDetails user1 = new UserDetails();
UserDetails user2 = new UserDetails();
user1.setUserId(1);
user1.setUserName(“Dinesh Rajput”);
user2.setUserId(2);
user2.setUserName(“Anamika Rajput”);
// Create Session Factory Object – using annotation configuration object
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
//Create Session object from session factory object
Session session = sessionFactory.openSession();
session.beginTransaction();
//Use the session to save model objects
session.save(user1);
session.save(user2);
session.getTransaction().commit();
session.close();
}
}
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Hibernate: insert into USER_DETAILS ( USER_ID, USER_NAME) values (?, ?)
Now we get the following table schema…

here we are looking the table USER_DETAILS only updated not again created.
Now in the Next Chapter we will learn about how to retrieve an object from database.
<<Previous Chapter 8<< >>Next Chapter10>>