Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

Using Hibernate with CUBRID

Before we start, note that CUBRID will be accessed from Hibernate using the JDBC driver. If you have a firm knowledge of JDBC and Hibernate you will notice that there isn't any difference in accessing CUBRID as with any other JDBC-enabled database systems. However, configuring Hibernate to work with CUBRID requires some different steps, as for the moment CUBRID support is not officially supported by Hibernate out of the box and we have a custom packaged Hibernate distribution with the added support.

Required Software

To successfully replicate the following examples, it is necessary to install the following prerequisites:

  • HIBERNATE Library for CUBRID
  • SLF4J Library

  • Download Hibernate.
  • Download Hibernate Library for CUBRID.
  • Download SLF4J Library.


Setting up the Directory Structure and Environment

Create a Workspace

In this tutorial you will learn how to configure a simple project that works with CUBRID and Hibernate to access a sample database. Let's start by creating a directory structure:

$mkdir tutorial
$mkdir tutorial/lib
$mkdir tutorial/src

Unpack Hibernate

Once you have downloaded the Hibernate, unpack it into a temporary directory and copy the following libraries to tutorial/lib:

  • antlr-2.7.6.jar
  • cglib-2.2.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • javassist-3.9.0.GA.jar
  • jta-1.1.jar

Unpack a SLF4J

Once you have downloaded the SLF4J Library, unpack it into a temporary directory and copy the following libraries to tutorial/lib:

  • slf4j-api-1.6.0.jar
  • slf4j-simple-1.6.0.jar

Copy CUBRID JDBC and Hibernate Library for CUBRID

Next, we will need to add two CUBRID specific libraries to tutorial/lib:

  • JDBC- This is CUBRID's JDBC Driver, which comes together with CUBRID. It is located at $CUBRID/jdbc directory, the place where you have installed the CUBRID Server. Or you can download it from JDBC & Java Programming with CUBRID.
  • hibernate-core-3.6.5-Final.jar: This is a custom built Hibernate Library for CUBRID.
The directory structure of your project should look now like this:



Let's suppose the following scenario. We have a table where we keep a list of countries. Each country is identified by its 3-letters code, has a name, continent and a capital. We will use this table to insert/select/update/delete records using Hibernate.

Create a Database Table

Let's start the implementation by opening up CUBRID Manager and creating a new database named tutorial. In this database we will create this table named country. DDL should look like this:

CREATE TABLE country (
	`name` VARCHAR(40),
	capital VARCHAR(30),
	continent VARCHAR(10)


Let's also insert a couple of records into the database for a start:

INSERT INTO country  VALUES ('ROU', 'Romania', 'Bucharest', 'Europe');
INSERT INTO country  VALUES ('CAN', 'Canada', 'Ottawa', 'America');

Create a POJO Mapping Class

Let's create the directory layout for java packages.

$mkdir -p tutorial/src/com/cubrid/hibernate
$cd tutorial/src/com/cubrid/hibernate

Here we create the Java class com.cubrid.hibernate.Country in

package com.cubrid.hibernate;
import java.util.Date;

public class Country {
	private String code;
	private String name;
	private String continent;
	private String capital;

	public String getCode() {
		return code;

	public void setCode(String code) {
		this.code = code;

	public String getName() {
		return name;

	public void setName(String name) { = name;
	public String getContinent() {
		return continent;

	public void setContinent(String continent) {
		this.continent = continent;

	public String getCapital() {
		return capital;

	public void setCapital(String capital) { = capital;

Create a HibernateUtil Class

Next we need to create the HibernateUtil class that provide support for session management in file.

package com.cubrid.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
	private static org.hibernate.SessionFactory sessionFactory;

	public static SessionFactory getSessionFactory() {
		if (sessionFactory == null) {
		return sessionFactory;

	private static synchronized void initSessionFactory() {
		sessionFactory = new Configuration().configure().buildSessionFactory();

	public static Session getSession() {
		return getSessionFactory().openSession();

Create a Tutorial Class

Let's write now the skeleton of our application. Let's call it Tutorial in class Here we will write later all the operations done using Hibernate.

package com.cubrid.hibernate;

public class Tutorial {
	public static void main(String[] args) {
		System.out.println("Tutorial is starting");

Create an Ant Build Script

In order to compile and run our tutorial we will use ant. The build.xml script from the root of our directory structure should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<project name="hibernate-tutorial" default="run">

	<property name="sourcedir" value="src"/>
	<property name="targetdir" value="bin"/>
	<property name="libdir" value="lib"/>

	<path id="libraries">
		<fileset dir="${libdir}">
			<include name="*.jar"/>

	<target name="clean">
		<delete dir="${targetdir}"/>
		<mkdir dir="${targetdir}"/>

	<target name="compile" depends="clean, copy-resources">
		<javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries">

	<target name="copy-resources">
		<copy todir="${targetdir}">
			<fileset dir="${sourcedir}">
				<exclude name="**/*.java"/>

	<target name="run" depends="compile">
		<java fork="true" classname="com.cubrid.hibernate.Tutorial" classpathref="libraries">
			<classpath path="${targetdir}"/>
			<arg value="${action}"/>

You can run ant now and if everything is set up right, the output would be:

Buildfile: build.xml
  [delete] Deleting directory /home/cristiroma/workspace/tutorial/bin
  [mkdir] Created dir: /home/cristiroma/workspace/tutorial/bin

   [copy] Copied 3 empty directories to 2 empty directories under /home/cristiroma/workspace/tutorial/bin

   [javac] Compiling 3 source files to /home/cristiroma/workspace/tutorial/bin

   [java] Tutorial is starting

Total time: 0 seconds

Configuring Hibernate

We will configure Hibernate using hibernate.cfg.xml file. The content of the file is presented below. The file has to be placed in the src folder. As you can see, in bold are highlighted the CUBRID specific settings. The database JDBC connection URL differs from other databases as well as JDBC driver, and Hibernate dialect classes.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "">
		<property name="connection.url">jdbc:cubrid:</property>
		<property name="connection.username">dba</property>
		<property name="connection.password">test</property>
		<property name="connection.driver_class">cubrid.jdbc.driver.CUBRIDDriver</property>
		<property name="dialect">org.hibernate.dialect.CUBRIDDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		 <!-- JDBC connection pool (use the built-in) -->
		<property name="connection.pool_size">1</property>
		<property name="current_session_context_class">thread</property>
		 <!-- Mapping files will go here.... -->
		<mapping resource="com/cubrid/hibernate/country.hbm.xml"/>

It's time now to write the mapping for our country table. As you already know we have to create a country.hbm.xml file in tutorial/src/com/cubrid/hibernate directory, as is highlighted in the configuration above. The content of the file is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">
	<class name="com.cubrid.hibernate.Country" table="country">
		<id name="code" column="code" type="java.lang.String" length="3" />
		<property name="name" type="java.lang.String" length="40"/>
		<property name="capital" type="java.lang.String" length="30"/>
		<property name="continent" type="java.lang.String" length="10"/>

Common Operations

Now that all configuration is complete, here is the directory layout of our project:


In this chapter let's see how we accomplish few simple operations using Hibernate. Open again the class and write a query.

package com.cubrid.hibernate;

import org.hibernate.Session;
import java.util.List;
import java.util.Iterator;
import com.cubrid.hibernate.HibernateUtil;

public class Tutorial {
	public static void main(String[] args) {
		System.out.println("Tutorial is starting");

		// Selecting rows
		Session session = HibernateUtil.getSession();
		List<Country> countries = session.createCriteria(Country.class).list();
		Iterator<Country> it = countries.iterator();

		while(it.hasNext()) {
			Country country =;

Running this code using ant will result in the following output.

Buildfile: build.xml
  [delete] Deleting directory /home/cristiroma/workspace/tutorial/bin
  [mkdir] Created dir: /home/cristiroma/workspace/tutorial/bin

  [copy] Copying 2 files to /home/cristiroma/workspace/tutorial/bin 

  [javac] Compiling 3 source files to /home/cristiroma/workspace/tutorial/bin 
  [javac] Note: /home/cristiroma/workspace/tutorial/src/com/cubrid/hibernate/ uses unchecked or unsafe operations. 
  [javac] Note: Recompile with -Xlint:unchecked for details. 

  [java] Tutorial is starting 
  [java] 11 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.5-Final 
  [java] 12 [main] INFO org.hibernate.cfg.Environment - not found 
  [java] 15 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 
  [java] 19 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 
  [java] 93 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 
  [java] 93 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 
  [java] 154 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/cubrid/hibernate/Country.hbm.xml 
  [java] 392 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory 
  [java] 538 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
  [java] Hibernate: 
  [java]     select 
  [java]         this_.code as code0_0_, 
  [java] as name0_0_, 
  [java] as capital0_0_,
  [java]         this_.continent as continent0_0_  
  [java]     from 
  [java]         country this_ 
  [java] ROU 
  [java] CAN

As you can see, Hibernate is connecting to the tutorial database and selects the two records from country table. Deleting records from the table is also straightforward:

//Deleting rows
session.createQuery("delete from Country").executeUpdate();

The rest of the possibilities enabled by Hibernate, including advanced queries, joins etc. can be accomplished in Hibernate using CUBRID as database the same way you would do with other RDBMS.

Getting Help

If you have any difficulties, you can post your questions to java forum for CUBRID at

You are either using a very old browser or a browser that is not supported.
In order to browse you need to have one of the following browsers:

Internet Explorer: Mozilla Firefox: Google Chrome: