Connect to a Database Using Java Hibernate Driver

Making a Database Connection

The connection URL when using the Hibernate driver has a different driver identifier, com.nuodb.hib, but is otherwise the same as the JDBC driver. For example: jdbc:com.nuodb.hib://localhost/test. See NuoDB Connection URL.

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

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 due to the change in XML namespace and property prefixes.

  • Supports both JPA specific properties (prefix jakarta.persistence or javax.persistence) and also Hibernate specific properties (prefix hibernate).

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

JPA 2 (Hibernate 5) Example

Supports JPA properties using prefix javax.persistence.

<?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" />
            <property name="hibernate.dialect"
                      value="com.nuodb.hibernate.NuoDBDialect" />
            ...
        </properties>

    </persistence-unit>
</persistence>
JPA 3 (Hibernate 6) Example

Requires a different XML namespace and any JPA properties use the jakarta prefix:

<?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" />
            <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 (although you do not typically access the 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 your application:

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.

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.

SessionFactory factory = configuration.buildSessionFactory();

try ( Session session = factory.openSession() ) {
     begin transaction (optional for read-only access) ...
     ... do work with entities ...
     ... commit or rollback as necessary
}