How do I configure hibernate.cfg.xml for a simple application?

To configure hibernate.cfg.xml for a simple Hibernate application, you need to include the essential properties for Hibernate to interact with the database and map your entities.

Below is an example of hibernate.cfg.xml:

<!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="hibernate.connection.driver_class">org.h2.Driver</property>
        <property name="hibernate.connection.url">jdbc:h2:mem:testdb</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="hibernate.connection.password"></property>

        <!-- Hibernate dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>

        <!-- JDBC connection pool settings -->
        <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>

        <!-- Enable Hibernate's automatic table creation -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Show SQL logs in the console -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>

        <!-- Add entity mappings -->
        <mapping class="org.kodejava.hibernate.Student"/>

    </session-factory>
</hibernate-configuration>

Explanation of the Configuration

  1. Database Connection Settings:
    • hibernate.connection.driver_class: Specifies the JDBC driver class (e.g., org.h2.Driver for an H2 database, com.mysql.cj.jdbc.Driver for MySQL, etc.).
    • hibernate.connection.url: JDBC URL to connect to your database.
    • hibernate.connection.username and hibernate.connection.password: Database credentials.
  2. Dialect:
    • hibernate.dialect: Hibernate’s SQL dialect for the specific database (e.g., H2Dialect, MySQLDialect, PostgreSQLDialect, etc.).
  3. Connection Pool:
    • Configures a simple C3P0 connection pool with properties like min_size, max_size, and timeout.
  4. Schema Management:
    • hibernate.hbm2ddl.auto:
      • create: Creates the schema, destroying any existing data.
      • update: Updates the schema, keeping existing data.
      • validate: Validates the schema without making changes.
      • none: Disables automatic schema management.
  5. SQL Output:
    • hibernate.show_sql: Logs executed SQL statements to the console.
    • hibernate.format_sql: Formats SQL logs for better readability.
  6. Entity Mapping:
    • <mapping class="org.kodejava.hibernate.Student"/>: Maps the Student entity to the database.

Using this Configuration

Place the hibernate.cfg.xml file in the src/main/resources directory (or in the root of your classpath). You can then use it to build a SessionFactory in your application:

SessionFactory factory = new Configuration()
        .configure("hibernate.cfg.xml")  // Load the config file
        .addAnnotatedClass(Student.class)  // Add annotated entity classes
        .buildSessionFactory();

This will allow Hibernate to connect to the database and manage your entities as per the configuration specified.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.