Connect to a Database Using Java Hibernate Driver

Making a Database Connection

The connection details when using the Hibernate driver are the same as the JDBC driver except it has a different database identifier and driver class name.

  • Database identifier is com.nuodb.hib, for example jdbc:com.nuodb.hib://localhost/test.

  • The driver class name is: com.nuodb.hibernate.NuoHibernateDriver.

  • For more details refer to NuoDB JDBC Connection URL.

All the examples below explicitly set the Dialect to be NuoDB’s dialect. To use the Dialect Resolver facility, do not specify the hibernate.dialect property and Hibernate will discover the dialect for itself.
This has the useful side-effect that Hibernate also knows the version of NuoDB being used and our dialect logs that information. To access the database version in code, invoke dialect.getVersion().

JPA with Hibernate

JPA configuration is performed by the META-INF/persistence.xml file which must be on the class path.

  • The file is slightly different between JPA 2 and 3 (or later) due to the change in XML namespace and property prefixes:

    • Up to JPA 2, JPA properties are prefixed by javax.persistence.

    • From JPA 3 the prefix changed to jakarta.persistence.

  • Hibernate specific properties are also supported, prefixed by hibernate.

In either case, the NuoDB specific properties are URL, dialect and driver class - see below.

JPA 2 Example

Supports JPA properties using prefix javax.persistence; corresponds to Hibernate 5:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- The configuration file for JPA 2.1 -->
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemalocation="http://xmlns.jcp.org/xml/ns/persistence
                        http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <!-- Each persistence unit correspond to a database -->
    <persistence-unit name="Hockey" transaction-type="RESOURCE_LOCAL">
        <class>com.nuodb.docs.Player</class>
        <properties>
            <!-- JPA Properties -->
            <property name="javax.persistence.jdbc.driver"
                      value="com.nuodb.hibernate.NuoHibernateDriver" />
            <property name="javax.persistence.jdbc.url"
                      value="jdbc:com.nuodb.hib://localhost/test" />
            <property name="javax.persistence.jdbc.user" value="dba" />
            <property name="javax.persistence.jdbc.password" value="goalie" />

            <!-- HIBERNATE Properties -->
            <property name="hibernate.connection.schema" value="Hockey" />
                <!-- Specifying the dialect is optional -->
            <property name="hibernate.dialect"
                      value="com.nuodb.hibernate.NuoDBDialect" />
            ...
        </properties>

    </persistence-unit>
</persistence>
JPA 3 (or later) Example

Requires a different XML namespace and any JPA properties use the jakarta prefix; corresponds to Hibernate 6 or later:

<?xml version="1.0" encoding="UTF-8" ?>

<!-- The configuration file for JPA 3 -->
<persistence version="3.0"
    xmlns="https://jakarta.ee/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemalocation="https://jakarta.ee/xml/ns/persistence
                        https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd">

    <!-- Each persistence unit correspond to a database -->
    <persistence-unit name="Hockey" transaction-type="RESOURCE_LOCAL">
        <class>com.nuodb.docs.Player</class>
        <properties>
            <!-- JPA Properties -->
            <property name="jakarta.persistence.jdbc.driver"
                      value="com.nuodb.hibernate.NuoHibernateDriver" />
            <property name="jakarta.persistence.jdbc.url"
                      value="jdbc:com.nuodb.hib://localhost/test" />
            <property name="jakarta.persistence.jdbc.user" value="dba" />
            <property name="jakarta.persistence.jdbc.password" value="goalie" />

            <!-- HIBERNATE Properties -->
            <property name="hibernate.connection.schema" value="Hockey" />
                <!-- Specifying the dialect is optional -->
            <property name="hibernate.dialect"
                      value="com.nuodb.hibernate.NuoDBDialect" />
            ...
        </properties>

    </persistence-unit>
</persistence>

To access the database requires an EntityManagerFactory. Its construction requires the name of the persistence unit whose configuration it should use:

EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Hockey");

All data access occurs via the EntityManager which also supports transaction management. Every EntityManager creates a connection for itself (it is possible, but not usual, to access this connection directly):

EntityManager em = entityManagerFactory.createEntityManager();

NuoDB with JPA, Hibernate, Spring and Spring Boot

For an example of using JPA and Hibernate with Spring and Spring Boot, refer to the examples.

Hibernate Session API

In pure Hibernate (without JPA), database connections are configured using a configuration file. Below are the relevant properties to set in hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">com.nuodb.hibernate.NuoDBDialect</property>
        <property name="hibernate.connection.driver_class">com.nuodb.hibernate.NuoHibernateDriver</property>
        <property name="hibernate.connection.url">jdbc:com.nuodb.hib://localhost/test</property>
        <property name="hibernate.connection.username">dba</property>
        <property name="hibernate.connection.password">goalie</property>
        <property name="hibernate.connection.schema">sample</property>
        ...
    </session-factory>
</hibernate-configuration>
The hibernate. prefix to these property names is optional. Thus hibernate.connection.url and connection.url are equivalent. If hibernate.cfg.xml does not exist, Hibernate looks for the same properties in hibernate.properties instead.

To configure Hibernate in application code:

Configuration configuration = new Configuration();

// Configure using the contents of 'hibernate.cfg.xml'
configuration.configure();

NuoDB Specific Properties

These are the property settings specific to NuoDB:

Property Description Example

dialect

Fully qualified class name for the NuoDB Hibernate Dialect (optional).

Disables Dialect Resolution when set.

com.nuodb.hibernate.NuoDBDialect

connection.driver_class

Fully qualified class name for the Hibernate Driver class.

com.nuodb.hibernate.NuoHibernateDriver

connection.url

Very similar to the NuoDB JDBC Connection String, except the subprotocol name (driver identifier) is com.nuodb.hib instead of com.nuodb.

jdbc:com.nuodb.hib://localhost/test

Accessing the Database

A JDBC connection corresponds to a Hibernate Session.

// Application scoped singleton
SessionFactory factory = configuration.buildSessionFactory();

// Create a session to access data. Sessions support transaction semantics.
try ( Session session = factory.openSession() ) {
     begin transaction (optional for read-only access) ...
     ... do work with entities ...
     ... commit or rollback as necessary
}