A project using Netbeans 7, JUnit, Maven, HSQLDB, Spring and Hibernate

Post to Twitter

I was reading a blog article the other day and decided to try the code out myself, however, I wanted to do it my way so I changed several things along the way. This article will outline what I did to get Netbeans 7 working with Spring, Hibernate, HSQLDB, JUnit and Maven all working together. The original code for the original article can be found here.

I began my project by creating a new Maven project called: SimpleSpringHibernate.

I added the following dependencies:

Note: Keep in mind you only really need to add Hibernate, Spring Framework, JUnit, HSQLDB, and the rest will be pulled in by Maven. I’ve included the above screen shot though just to to show everything as I had it configured.

I created just one entity called Employee which takes an id, name and a department. It is wired up for Hibernate and fairly simple to understand.


package com.giantflyingsaucer.simplespringhibernate.entity;

import javax.persistence.*;
import java.io.Serializable;
import org.hibernate.annotations.GenericGenerator;

@Table(name = "Employees")
public class Employee implements Serializable {

	private String employeeId;
	private String name;
	private String department;

	@GeneratedValue(generator = "system-uuid")
	@GenericGenerator(name = "system-uuid", strategy = "uuid")
	@Column(name = "EmployeeId", nullable=false)
	public String getEmployeeId() {
		return this.employeeId;

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	@Column(name = "Name", nullable=false)
	public String getEmployeeName() {
		return this.name;

	public void setEmployeeName(String name) {
		this.name = name;
	@Column(name = "Department", nullable=false)
	public String getEmployeeDepartment() {
		return this.department;

	public void setEmployeeDepartment(String department) {
		this.department = department;

After that and according to the original article I built the interface for the EmployeeDAO.


package com.giantflyingsaucer.simplespringhibernate.dao;

import java.util.List;
import com.giantflyingsaucer.simplespringhibernate.entity.Employee;

public interface EmployeeDAO {
	public void saveEmployee(Employee employee);
	public List getAllEmployees(Employee employee);
	public Employee getEmployeeById(String employeeId);
	public void deleteEmployee(Employee employee);

The actual implementation code looks like this:


package com.giantflyingsaucer.simplespringhibernate.dao;

import com.giantflyingsaucer.simplespringhibernate.entity.Employee;

import java.util.List;

import org.hibernate.SessionFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

public class EmployeeDAOImpl implements EmployeeDAO {
	private HibernateTemplate hibernateTemplate;

	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);

	@Transactional(readOnly = false)
	public void saveEmployee(Employee employee) {

	@Transactional(readOnly = false)
	public void deleteEmployee(Employee employee) {

	public List getAllEmployees(Employee employee) {
		return (List) hibernateTemplate.find("from "
				+ Employee.class.getName());

	public Employee getEmployeeById(String employeeId) {
		return hibernateTemplate.get(Employee.class, employeeId);

My JUnit test code looks like this:


package com.giantflyingsaucer.simplespringhibernate;

import java.util.List;

import org.junit.Test;
import org.junit.Assert;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.giantflyingsaucer.simplespringhibernate.dao.*;
import com.giantflyingsaucer.simplespringhibernate.entity.Employee;

@ContextConfiguration( { "/app-config.xml" })
public class EmployeeTest {
	private EmployeeDAO dao;

	public void setDao(EmployeeDAO dao) {
		this.dao = dao;

	public void testCreateData() {
		int expectedResult = 1;
		Employee employee = new Employee();
		employee.setEmployeeDepartment("Human Resources");
		employee.setEmployeeName("Barry Jones");
		Assert.assertEquals(expectedResult, dao.getAllEmployees(new Employee()).size());

	public void testRetrieveData() {
		List employeeList = dao.getAllEmployees(new Employee());
		Assert.assertEquals(1, employeeList.size());
		Employee employeeExpected = employeeList.get(0);
		Employee employeeResult = dao.getEmployeeById(employeeExpected.getEmployeeId());
		Assert.assertEquals(employeeExpected.getEmployeeId(), employeeResult.getEmployeeId());

	public void testUpdateData() {
		List employeeList = dao.getAllEmployees(new Employee());
		Assert.assertEquals(1, employeeList.size());
		Employee employeeExpected = employeeList.get(0);
		employeeExpected.setEmployeeName("Jim Jones");
		Employee employeeResult = dao.getEmployeeById(employeeExpected.getEmployeeId());
		Assert.assertEquals(employeeExpected.getEmployeeName(), employeeResult

	public void testDeleteData() {
		List employeeList = dao.getAllEmployees(new Employee());
		Assert.assertEquals(1, employeeList.size());
		Employee employeeExpected = employeeList.get(0);
		Employee employeeResult = dao.getEmployeeById(employeeExpected.getEmployeeId());
		Assert.assertEquals(employeeResult, null);

All that’s left is three configuration files needed mostly by Spring. These files go into the resources folder.







Note: I’m using HSQLDB here but you could swap that out for any of the Hibernate supported databases.



My project layout looks like this:

Clean and build the project and all four tests should pass.

You can find the project files here.

Post to Twitter

This entry was posted in Java, Netbeans, Open Source. Bookmark the permalink.

2 Responses to A project using Netbeans 7, JUnit, Maven, HSQLDB, Spring and Hibernate

  1. Mike says:

    Hi. Nice post. I’ve tried running this in Netbeans, using your code and I consistently get a ‘java.lang.ClassNotFoundException: org.hsqldb.jdbcDriver’ error when I build.

    For some reason, it is not able to load this driver. I have the jar in my dependencies. Any idea on why this would happen?


  2. Víctor says:

    Nice Post.

    I have some problems with “Failed to load ApplicationContext”, but I have learned a lot from your sample.


Comments are closed.