Hibernate and HSQLDB

This page describes how to setup a project using Hibernate and HSQLDB. A better method would be to use JPA to configure hibernate and use the JPA API directly.


  • Java 5
  • Maven
  • Understanding of JDBC and SQL
  • Basic understanding of Relational Databases

Start a new project

Run the following command to generate a new project using a maven archetype.

mvn archetype:generate --batch-mode \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DgroupId=com.test \

The following maven pom.xml file defines the following dependencies

  • Hibernate and Hibernate Annotations


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">



Hibernate Configuration File

The following configuration files defines

  • Connection URL that specifies that the HSQLDB database will be file based, it will be located in the src/main/resources/db folder and the “shutdown” parameter specifies that the database will be persisted to the file when all the connections are closed.
  • Default admin username is “sa” and password is “blank”
  • The connection pooling is turned off by specifying “connection.pool_size” as 0 (hibernates built in pool implementation is really bad. It prevents HSQLDB from properly shutting down. Therefore we turn it off.


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    <property name="connection.url">jdbc:hsqldb:file:src/main/resources/db/mydbname;shutdown=true</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>

    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

    <property name="show_sql">true</property>

    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    * validate: validate the schema, makes no changes to the database.
    * update: update the schema.
    * create: creates the schema, destroying previous data.
    * create-drop: drop the schema at the end of the session.
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">0</property>
    <property name="current_session_context_class">thread</property>

<mapping class="com.test.hibernate.model.Blog" />


Entity Bean


package com.test.hibernate.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;

public class Blog {

  private Long id;

  private String subject;

  private String body;

  private Date createdAt;

  public Long getId() {
    return id;

  public void setId(Long id) {
    this.id = id;

  public String getSubject() {
    return subject;

  public void setSubject(String subject) {
    this.subject = subject;

  public String getBody() {
    return body;

  public void setBody(String body) {
    this.body = body;

  public Date getCreatedAt() {
    return createdAt;

  public void setCreatedAt(Date createdAt) {
    this.createdAt = createdAt;

Test case


package com.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import com.test.hibernate.model.Blog;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class AppTest extends TestCase {
    public AppTest(String testName) {

    public static Test suite() {
        return new TestSuite(AppTest.class);

    public void testHibernate() throws Exception {
        SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();

        Session session = sessionFactory.getCurrentSession();

        Transaction tx = session.beginTransaction();

        Blog b = new Blog();

// the connection pool implementation in hibernate is bad.
// Either use 3cpo or dbcp or dont use it at all...
// in this example we set the connection pooling off.
// <property name="connection.pool_size">0</property>


Run the test case

mvn clean compile test

Related Posts


To shutdown the HSQLDB database manually you can issue the following.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 77 other followers

June 2010
« May   Aug »

Blog Stats

  • 846,580 hits

%d bloggers like this: