Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

Hibernate Configuration

Key Configuration Properties

PropertyValuesDescription
hibernate.dialectMySQLDialect, PostgreSQLDialect, etc.Database-specific SQL generation
hibernate.hbm2ddl.autovalidate, update, create, create-drop, noneSchema management strategy
hibernate.show_sqltrue/falsePrint generated SQL to console
hibernate.format_sqltrue/falseFormat SQL for readability
hibernate.connection.pool_sizeintegerBuilt-in connection pool size
hibernate.cache.use_second_level_cachetrue/falseEnable L2 cache
hibernate.jdbc.batch_sizeinteger (e.g., 50)JDBC batch insert/update size
Complete hibernate.cfg.xml with C3P0
<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

Programmatic Hibernate 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.