About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

The minimal configuration for testing Hibernate

Introduction

In my previous post I announced my intention of creating a personal Hibernate course. The first thing to start with is a minimal testing configuration. The examples are relevant for Hibernate 4.

You only need Hibernate

In a real production environment you won’t use Hibernate alone, as you may integrate it in a JEE or Spring container. For testing Hibernate features you don’t need a full-blown framework stack, you can simply rely on Hibernate flexible configuration options.

Case 1: Driver based JDBC configuration

We first define a test Entity:

@Entity
class SecurityId {
    @Id
    @GeneratedValue
    private Long id;

    private String role;

    public Long getId() {
        return id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }
}

Thanks to Hibernate Transaction abstraction layer we are not forced to employ any external transaction manager or have to write any home-made transaction management code.

For testing purposes we can use the JDBC resource local transactions, which are managed internally by the default JdbcTransactionFactory.

We don’t even need to supply an external data source, as Hibernate is supplied with a non-production built-in connection pool represented by DriverManagerConnectionProviderImpl.

Our test code looks like this:

@Test
public void test() {
	Session session = null;
	Transaction txn = null;
	try {
		session = sf.openSession();
		txn = session.beginTransaction();

		SecurityId securityId = new SecurityId();
		securityId.setRole("Role");
		session.persist(securityId);

		txn.commit();
	} catch (RuntimeException e) {
		if ( txn != null && txn.isActive() ) txn.rollback();
		throw e;
	} finally {
		if (session != null) {
			session.close();
		}
	}
}

We don’t need any external configuration file, so this is how we can build and configure a session factory:

@Override
protected SessionFactory newSessionFactory() {
	Properties properties = new Properties();
	properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
	//log settings
	properties.put("hibernate.hbm2ddl.auto", "update");
	properties.put("hibernate.show_sql", "true");
	//driver settings
	properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
	properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
	properties.put("hibernate.connection.username", "sa");
	properties.put("hibernate.connection.password", "");

	return new Configuration()
			.addProperties(properties)
			.addAnnotatedClass(SecurityId.class)
			.buildSessionFactory(
					new StandardServiceRegistryBuilder()
							.applySettings(properties)
							.build()
	);
}

Case 2: Using a professional connection pool

If we want to replace the built-in connection pool with a professional one, Hibernate offers the choice of setting up c3p0 which is handled internally by C3P0ConnectionProvider.

We only need to change the session factory configuration properties:

protected SessionFactory newSessionFactory() {
	Properties properties = new Properties();
	properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
	//log settings
	properties.put("hibernate.hbm2ddl.auto", "update");
	properties.put("hibernate.show_sql", "true");
	//driver settings
	properties.put("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
	properties.put("hibernate.connection.url", "jdbc:hsqldb:mem:test");
	properties.put("hibernate.connection.username", "sa");
	properties.put("hibernate.connection.password", "");
	//c3p0 settings
	properties.put("hibernate.c3p0.min_size", 1);
	properties.put("hibernate.c3p0.max_size", 5);

	return new Configuration()
			.addProperties(properties)
			.addAnnotatedClass(SecurityId.class)
			.buildSessionFactory(
					new StandardServiceRegistryBuilder()
							.applySettings(properties)
							.build()
	);
}

Case 3: Using an external data-source

Since Hibernate doesn’t log SQL prepared statements parameters:

o.h.SQL - insert into SecurityId (id, role) values (default, ?)

We will add a datasource-proxy to intercept the actual SQL queries:

n.t.d.l.SLF4JQueryLoggingListener - Name: Time:0 Num:1 Query:{[insert into SecurityId (id, role) values (default, ?)][Role]}

The configuration looks like this:

@Override
protected SessionFactory newSessionFactory() {
	Properties properties = new Properties();
	properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
	//log settings
	properties.put("hibernate.hbm2ddl.auto", "update");
	//data source settings
	properties.put("hibernate.connection.datasource", newDataSource());

	return new Configuration()
			.addProperties(properties)
			.addAnnotatedClass(SecurityId.class)
			.buildSessionFactory(
					new StandardServiceRegistryBuilder()
							.applySettings(properties)
							.build()
			);
}

private ProxyDataSource newDataSource() {
	JDBCDataSource actualDataSource = new JDBCDataSource();
	actualDataSource.setUrl("jdbc:hsqldb:mem:test");
	actualDataSource.setUser("sa");
	actualDataSource.setPassword("");
	ProxyDataSource proxyDataSource = new ProxyDataSource();
	proxyDataSource.setDataSource(actualDataSource);
	proxyDataSource.setListener(new SLF4JQueryLoggingListener());
	return proxyDataSource;
}

Conclusion

This is the minimal configuration set-up we need for testing Hibernate features. I also use these configurations whenever I submit a Hibernate bug report accompanied by a replicating test case.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× 6 = thirty



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close