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.
Requirements
- 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 \ -DartifactId=hsqldbTest
The following maven pom.xml file defines the following dependencies
- HSQLDB
- Hibernate and Hibernate Annotations
pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>hsqldbTest</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>hsqldbTest</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.GA</version>
</dependency>
</dependencies>
</project>
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.
src/main/resources/hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<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" />
</session-factory>
</hibernate-configuration>
Entity Bean
src/main/java/com/test/hibernate/model/Blog.java
package com.test.hibernate.model;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
@Entity
public class Blog {
private Long id;
private String subject;
private String body;
private Date createdAt;
@Id
@GeneratedValue
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
src/test/java/com/test/AppTest.java
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) {
super(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();
session.save(b);
tx.commit();
// 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
Appendix
To shutdown the HSQLDB database manually you can issue the following.
//org.hsqldb.DatabaseManager.closeDatabases(0);
1 Response to “Hibernate and HSQLDB”