Connecting 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.
Below are the relevant properties to set.
Note that this file supports both JPA specific properties (prefix javax.persistence
) and also Hibernate specific properties.
<?xml version="1.0" encoding="UTF-8" ?>
<!-- The configuration file for JPA -->
<persistence
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<!-- 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>
To access the database requires an EntityManagerFactory
.
Its constructor takes 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.
|
To configure Hibernate in your application:
Configuration configuration = new Configuration();
configuration.configure();
NuoDB Specific Properties
These are the property settings specific to NuoDB:
Property | Description | Example |
---|---|---|
|
Fully qualified class name for the NuoDB Hibernate Dialect. |
|
|
Fully qualified class name for the Hibernate Driver class. |
|
|
Very similar to the NuoDB JDBC Connection String, except the subprotocol name (driver identifier) is |
|