Connecting to a Database Using Java Hibernate Driver

Making a Database Connection

In Hibernate, database connections can be configured using a properties file. Below is the hibernate.cfg.xml file that comes with the NuoDB Hibernate example:

<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="dialect">com.nuodb.hibernate.NuoDBDialect</property>
        <property name="connection.driver_class">com.nuodb.hibernate.NuoHibernateDriver</property>
        <property name="connection.url">jdbc:com.nuodb.hib://localhost/test</property>
        <property name="hibernate.connection.schema">sample</property>
        <!-- Use the Hibernate built-in pool for tests. -->
        <property name="connection.pool_size">3</property>
    <!-- NuoDB currently doesn't support this but this is required to be false for Hibernate version 4.x -->
    <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="cache.use_query_cache">false</property>
        <property name="cache.use_minimal_puts">false</property>
        <!-- In eager fetching, only join three tables deep if joins are used -->
        <property name="max_fetch_depth">3</property>
        <!-- Print SQL to stdout, format it nicely  -->
        <property name="show_sql">false</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>
        <!-- Drop and then re-create schema on SessionFactory build, for testing  -->
        <property name="hbm2ddl.auto">create</property>
        <!-- Use thread-bound persistence context propagation, scoped to the transaction -->
        <property name="current_session_context_class">thread</property>
        <!-- CaveatEmptor mapping files -->
        <mapping resource="com/nuodb/sample/model/User.hbm.xml"/>
        <mapping resource="com/nuodb/sample/model/AddressEntity.hbm.xml"/>
    </session-factory>
</hibernate-configuration> 

NuoDB Properties

These are the properties specific to NuoDB:

Property Description Example
dialect Fully qualified class name for the NuoDB Hibernate Dialect com.nuodb.hibernate.NuoDBDialect
connection.driver_class Fully qualified class name for the JDBC Driver class com.nuodb.hibernate.NuoHibernateDriver
connection.url Similar to the NuoDB JDBC Connection String; the difference is the subprotocol name is "com.nuodb.hib" versus "com.nuodb" jdbc:com.nuodb.hib://localhost/test

A JDBC connection is then obtained with:

Session session = sessions.openSession();