Hibernate Configuration
Key Configuration Properties
| Property | Values | Description |
|---|---|---|
| hibernate.dialect | MySQLDialect, PostgreSQLDialect, etc. | Database-specific SQL generation |
| hibernate.hbm2ddl.auto | validate, update, create, create-drop, none | Schema management strategy |
| hibernate.show_sql | true/false | Print generated SQL to console |
| hibernate.format_sql | true/false | Format SQL for readability |
| hibernate.connection.pool_size | integer | Built-in connection pool size |
| hibernate.cache.use_second_level_cache | true/false | Enable L2 cache |
| hibernate.jdbc.batch_size | integer (e.g., 50) | JDBC batch insert/update size |
<hibernate-configuration>
<session-factory>
<!-- Connection -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!-- Dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- DDL -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Logging -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<!-- C3P0 Connection Pool -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- Batch processing -->
<property name="hibernate.jdbc.batch_size">50</property>
<property name="hibernate.order_inserts">true</property>
<property name="hibernate.order_updates">true</property>
<!-- Entity mappings -->
<mapping class="com.example.entity.User"/>
<mapping class="com.example.entity.Product"/>
</session-factory>
</hibernate-configuration>
Programmatic Configuration
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.Properties;
public class HibernateConfig {
public static SessionFactory buildSessionFactory() {
Properties props = new Properties();
// Connection settings
props.setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver");
props.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/mydb");
props.setProperty("hibernate.connection.username", "root");
props.setProperty("hibernate.connection.password", "password");
// Dialect
props.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
// DDL
props.setProperty("hibernate.hbm2ddl.auto", "update");
// Logging
props.setProperty("hibernate.show_sql", "true");
props.setProperty("hibernate.format_sql", "true");
Configuration config = new Configuration();
config.setProperties(props);
// Add annotated entity classes
config.addAnnotatedClass(com.example.entity.User.class);
config.addAnnotatedClass(com.example.entity.Product.class);
return config.buildSessionFactory();
}
}
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.