Hibernate can automatically create and manage database tables using configuration settings, reducing the need to write SQL manually. This feature is controlled by the hibernate.hbm2ddl.auto property, which defines how schema generation should behave. It is especially useful during development and testing for quick database setup and updates.
- Supports multiple modes like create, update, validate, etc.
- Speeds up development by automatically syncing entity changes with the database
- Reduces boilerplate code, no need to write table creation queries manually
Syntax (Configuration Property):
<property name="hibernate.hbm2ddl.auto">value</property>
"hibernate.hbm2ddl.auto" property accepts the following values:
- create –> Drops the existing schema (tables) and recreates it from entity mappings at startup.
- validate –> Validates the database schema against entity mappings (checks table structure, columns, types) without making any changes.
- none –> Hibernate performs no schema generation, update, or validation; it completely ignores database schema management
- create-drop –> Creates and drops table automatically
- update –> Updates existing table
Step-by-Step Project Implementation
Follow below steps to demonstrating the automatic table creation in hibernate using the MySQL database.
Step 1: Create Java Project
Create a simple Java project (e.g., in Eclipse/IntelliJ) and Add Hibernate and MySQL dependencies (JARs or Maven)
Step 2: Create Database
Create a database in MySQL:
CREATE DATABASE demo;
Step 3: Create Hibernate Configuration File
Create a hibernate configuration file hibernate.cfg.xml file.
hibernate.cfg.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate Configuration DTD 3.0//EN"
"https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<mapping resource="Student.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
Step 4: Create Mapping File
Create a mapping file Student.hbm.xml file.
Student.hbm.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="p1.Student" table="student">
<id name="id"></id>
<property name="name"></property>
</class>
</hibernate-mapping>
Step 5: Create Entity Class
Create the Entity Class named as Student.java.
Student.java file:
package p1;
public class Student {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 6: Create Main Class
Create Main class Test.java to write the logic .
Test.java file:
package p1;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import p1.Student;
public class Test {
public static void main(String... args) {
try {
Configuration config = new Configuration();
config.configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
// start transaction first
Transaction t = session.beginTransaction();
Student s = new Student();
s.setId(101);
s.setName("Raghav");
session.save(s);
// Commit after operation
t.commit();
session.close();
sessionFactory.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
Output:
A new table 'student' is created and data from the Student class object is mapped into the table.