Hibernate Configuration

Maven POM for Hibernate Application

Hibernate POM file (pom.xml)

  • Located at the project root directory (project/)
  • Maven with the following POM file is used to compile and build a Hibernate Application
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.innotrekker.app</groupId>
        <artifactId>hibernate</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>pom</packaging>
    
        <name>Hibernate App</name>
        <description>Hibernate Demonstration Application</description>
    
        <build>
             <finalName>${artifactId}</finalName>
        </build>
    
        <dependencies>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-simple</artifactId>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </dependency>
    
            <dependency>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
            </dependency>
    
        </dependencies>
    
        <build>
            <testResources>
                <testResource>
                    <filtering>false</filtering>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml</include>
                    </includes>
                </testResource>
                <testResource>
                    <filtering>true</filtering>
                    <directory>src/test/resources</directory>
                </testResource>
            </testResources>
        </build>
    
    </project>
  • Compile the application using Maven
    mvn compile

Hibernate Configuration File

Hibernate Configuration file using Oracle DB - hibernate.cfg.xml

Create hibernate.cfg.xml under src/main/resources

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
        <property name="show_sql" value="true"/>

        <property name="connection.driver_class" value="oracle.jdbc.OracleDriver"/>
        <property name="connection.url" value="jdbc:oracle:thin:@myhost:1521:mydb"/>
        <property name="connection.username" value="myuser"/>
        <property name="connection.password" value="secret"/>

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <mapping resource="com/innotrekker/app/hbm/Order.hbm.xml"/>
        <mapping resource="com/innotrekker/app/hbm/Item.hbm.xml"/>

    </session-factory>

</hibernate-configuration>
  • To support multiple DB, use a separate Hibernate configuration for each DB

Hibernate Configuration file using MySQL DB - hibernate.cfg.xml

...
	<property name="connection.url">jdbc:mysql://localhost/mydb</property>
	<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="connection.username">myuser</property>
	<property name="connection.password">secret</property>
...

Using C3P0 DB connection pool for Hibernate

<property name="c3p0.acquire_increment">5</property>
<property name="c3p0.min_size">10</property>
<property name="c3p0.max_size">100</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.timeout">180</property>
<property name="c3p0.idle_test_period">180</property>

Using Java Container Datasource for Hibernate

<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.datasource">java:comp/env/jdbc/mydataSource</property>

Tomcat server.xml

<Context path="/app" docBase="app"
        debug="5" reloadable="true" crossContext="true">

  <Resource name="jdbc/mydatasource" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="10" maxWait="10000"
               username="myuser" password="secret" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://host:3306/mydb"/>

</Context>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
  <description>My App</description>
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mydatasource</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

Hibernate Configuration Options

Hibernate Configuration Options Value Description
dialect org.hibernate.dialect.Oracle10gDialect, org.hibernate.dialect.MySQLDialect SQL Dialect to be used
show_sql true or false Show all executed SQL to console
format_sql true or false Output the sql in a pretty format during logging
id.new_generator_mappings true or false Set to true allows Hibernate to use a better sequence generator for @GeneratedValue. Use this only for a new project with a new schema
use_sql_comments true or false Generate automatic SQL comment in the SQLs for easier tracing
order_updates true or false Order the order of updates by primary key to reduce deadlock possibilities
generate_statistics true or false Generate statistics for performance tuning
max_fetch_depth 0-3: Value of 1 or more uses SQL Outer join if needed for query. 0 means disable outer join which may results more SQL queries for the same request

Hibernate JDBC Options

Hibernate JDBC Configuration Options Description
jdbc.fetch_size JDBC Fetch size
jdbc.batch_size Enable and set JDBC batch size
jdbc.use_scrollable_resultset Enable scrollable resultset
jdbc.use_get_generated_keys Enable the use of JDBC 3.0 PreparedStatement.getGeneratedKeys() to retrieve the generated primary key
connection.isolation Set transaction isolation
connection.autocommit Set auto commit
jdbc.use_streams_for_binary Use binary stream to support large binary data

Hibernate Cache Options

Hibernate Cache Options Description
cache.use_query_cache Enable query cached when specific query is set to be cacheable
cache.use_second_level_cache Use to disable cache completely

Hibernate Transaction Options

Hibernate Transaction Options Description
jta.UserTransaction JNDI name for the container implementation of JTA
hibernate.transaction.factory_class Transaction factory class

Hibernate Transaction Factory Class Value

org.hibernate.transaction.JBossTransactionManagerLookup JBoss AS
org.hibernate.transaction.WeblogicTransactionManagerLookup Weblogic
org.hibernate.transaction.WebSphereTransactionManagerLookup WebSphere
org.hibernate.transaction.WebSphereExtendedJTATransactionLookup WebSphere 6

Hibernate SQL dialects

SQL dialect value in the Hibernate configuration

DB dialect value
MySQL org.hibernate.dialect.MySQLDialect
MySQL (InnoDB) org.hibernate.dialect.MySQLInnoDBDialect
MySQL (MyISAM) org.hibernate.dialect.MySQLMyISAMDialect
Oracle (general) org.hibernate.dialect.OracleDialect
Oracle 9i org.hibernate.dialect.Oracle9iDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect

Hibernate Logging Catalog

Set the appropriate logging level for the catalog below for system monitoring and trouble shooting

Catalog Description
org.hibernate.SQL Log all SQL DML statements
org.hibernate.type Log all JDBC parameters
org.hibernate.tool.hbm2ddl Log all SQL DDL statements
org.hibernate.pretty Log all entities associated with the session at flush time
org.hibernate.cache Log second-level cache
org.hibernate.transaction Log transaction
org.hibernate.jdbc Log all JDBC resource
org.hibernate.hql.ast.AST Log HQL and SQL ASTs
org.hibernate Log everything

Hibernate Library

POM Name Function
hibernate-core Core Hibernate Library
hibernate-entitymanager JPA implementation
hibernate-envers Auditing changes to entities
hibernate-ehcache Hibernate and EhCache integration for caching