Hibernate Facts: How to “assert” the SQL statement count


Hibernate simplifies CRUD operations, especially when dealing with entity graphs. But any abstraction has its price and Hibernate is no different. I’ve already talked about the importance of fetching strategy and knowing your Criteria SQL queries, but there is more you can do to rule over JPA. This post is about controlling the SQL statement count that Hibernate calls on your behalf.

Before ORM tools got so popular, all database interactions were done through explicit SQL statements, and optimizations were mostly targeted towards slow queries.

Hibernate may give the false impression that you don’t need to worry about SQL statements. This is a wrong and dangerous assumption. Hibernate is supposed to ease domain model persistence, not to free you from any SQL interaction.

With Hibernate, you manage entity state transitions which are then translated to SQL statements. The number of generated SQL statements is affected by the current fetching strategy, Criteria queries or Collection mappings and you might not always get what you expected. Ignoring SQL statements is risky and it may eventually put a heavy toll on the overall application performance.

I’m a strong advocate of peer reviewing, but that’s not “sine qua non” for detecting bad Hibernate usage. Subtle changes may affect the SQL statement count and pass unnoticed through the reviewing process. Not in the least, when it comes to “guessing” the JPA SQL statements, I feel like I can use any extra help. I’m for as much automation as possible, and that’s why I came up with a mechanism for enforcing the SQL statement count expectations.

First, we need a way to intercept all executed SQL statements. I researched on this topic and I was lucky to find this great datasource-proxy library.

Adding an automated validator

This safeguard is meant to run only during the testing phase, so I’ll add it to the Integration Testing spring context exclusively. I’ve already talked about Spring bean aliasing and this is the right time to make use of it.

<bean id="testDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" init-method="init"
	<property name="className" value="bitronix.tm.resource.jdbc.lrc.LrcXADataSource"/>
	<property name="uniqueName" value="testDataSource"/>
	<property name="minPoolSize" value="0"/>
	<property name="maxPoolSize" value="5"/>
	<property name="allowLocalTransactions" value="false" />
	<property name="driverProperties">
			<prop key="user">${jdbc.username}</prop>
			<prop key="password">${jdbc.password}</prop>
			<prop key="url">${jdbc.url}</prop>
			<prop key="driverClassName">${jdbc.driverClassName}</prop>

<bean id="proxyDataSource" class="net.ttddyy.dsproxy.support.ProxyDataSource">
	<property name="dataSource" ref="testDataSource"/>
	<property name="listener">
		<bean class="net.ttddyy.dsproxy.listener.ChainListener">
			<property name="listeners">
					<bean class="net.ttddyy.dsproxy.listener.CommonsQueryLoggingListener">
						<property name="logLevel" value="INFO"/>
					<bean class="net.ttddyy.dsproxy.listener.DataSourceQueryCountListener"/>

<alias name="proxyDataSource" alias="dataSource"/>

The new proxy data source decorates the already existing data source, intercepting all executed SQL statements. This library can log all SQL statements along with the actual parameter values, unlike the default Hibernate logging which only prints a placeholder instead.

This is how the validator looks like:

public class SQLStatementCountValidator {

    private SQLStatementCountValidator() {

     * Reset the statement recorder
    public static void reset() {

     * Assert select statement count
     * @param expectedSelectCount expected select statement count
    public static void assertSelectCount(int expectedSelectCount) {
        QueryCount queryCount = QueryCountHolder.getGrandTotal();
        int recordedSelectCount = queryCount.getSelect();
        if(expectedSelectCount != recordedSelectCount) {
            throw new SQLSelectCountMismatchException(expectedSelectCount, recordedSelectCount);

     * Assert insert statement count
     * @param expectedInsertCount expected insert statement count
    public static void assertInsertCount(int expectedInsertCount) {
        QueryCount queryCount = QueryCountHolder.getGrandTotal();
        int recordedInsertCount = queryCount.getInsert();
        if(expectedInsertCount != recordedInsertCount) {
            throw new SQLInsertCountMismatchException(expectedInsertCount, recordedInsertCount);

     * Assert update statement count
     * @param expectedUpdateCount expected update statement count
    public static void assertUpdateCount(int expectedUpdateCount) {
        QueryCount queryCount = QueryCountHolder.getGrandTotal();
        int recordedUpdateCount = queryCount.getUpdate();
        if(expectedUpdateCount != recordedUpdateCount) {
            throw new SQLUpdateCountMismatchException(expectedUpdateCount, recordedUpdateCount);

     * Assert delete statement count
     * @param expectedDeleteCount expected delete statement count
    public static void assertDeleteCount(int expectedDeleteCount) {
        QueryCount queryCount = QueryCountHolder.getGrandTotal();
        int recordedDeleteCount = queryCount.getDelete();
        if(expectedDeleteCount != recordedDeleteCount) {
            throw new SQLDeleteCountMismatchException(expectedDeleteCount, recordedDeleteCount);

This utility is part of my db-util project along with the JPA and MongoDB optimistic concurrency control retry mechanism.

Since it’s already available in Maven Central Repository, you can easily use it by just adding this dependency to your pom.xml:


Let’s write a test for detecting the infamous N+1 select query problem.

For this we will write two service methods, one of them being affected by the aforementioned issue:

public List<WarehouseProductInfo> findAllWithNPlusOne() {
	List<WarehouseProductInfo> warehouseProductInfos = entityManager.createQuery(
			"from WarehouseProductInfo", WarehouseProductInfo.class).getResultList();
	return warehouseProductInfos;

public List<WarehouseProductInfo> findAllWithFetch() {
	List<WarehouseProductInfo> warehouseProductInfos = entityManager.createQuery(
			"from WarehouseProductInfo wpi " +
			"join fetch wpi.product p " +
			"join fetch p.company", WarehouseProductInfo.class).getResultList();
	return warehouseProductInfos;

private void navigateWarehouseProductInfos(List<WarehouseProductInfo> warehouseProductInfos) {
	for(WarehouseProductInfo warehouseProductInfo : warehouseProductInfos) {

If you like this article, I bet you are going to love my book as well.

The unit test is rather simple to write since it follows the same coding style of any other JUnit assert mechanism.

try {
} catch (SQLSelectCountMismatchException e) {
	assertEquals(3, e.getRecorded());


Our validator works for all SQL statement types, so let’s check how many SQL INSERTs are being executed by the following service method:

public WarehouseProductInfo newWarehouseProductInfo() {


	Company company = entityManager.createQuery("from Company", Company.class).getResultList().get(0);

	Product product3 = new Product("phoneCode");

	WarehouseProductInfo warehouseProductInfo3 = new WarehouseProductInfo();

	return warehouseProductInfo3;

And the validator looks like:


Let’s check the test logs to convince ourselves of its effectiveness:

INFO  [main]: o.v.s.i.WarehouseProductInfoServiceImpl - newWarehouseProductInfo
Hibernate: select company0_.id as id1_6_, company0_.name as name2_6_ from Company company0_
INFO  [main]: n.t.d.l.CommonsQueryLoggingListener - Name:, Time:1, Num:1, Query:{[select company0_.id as id1_6_, company0_.name as name2_6_ from Company company0_][]}
Hibernate: insert into WarehouseProductInfo (id, quantity) values (default, ?)
INFO  [main]: n.t.d.l.CommonsQueryLoggingListener - Name:, Time:0, Num:1, Query:{[insert into WarehouseProductInfo (id, quantity) values (default, ?)][19]}
Hibernate: insert into Product (id, code, company_id, importer_id, name, version) values (default, ?, ?, ?, ?, ?)
INFO  [main]: n.t.d.l.CommonsQueryLoggingListener - Name:, Time:0, Num:1, Query:{[insert into Product (id, code, company_id, importer_id, name, version) values (default, ?, ?, ?, ?, ?)][phoneCode,1,-5,Phone,0]}


Code reviewing is a fine technique, but it’s not enough on large scale development projects. That’s why automatic checking is of paramount importance. Once the test written, you are assured that no future change can break your assumptions.

Code available on GitHub.

If you liked this article, you might want to subscribe to my newsletter too.

2 thoughts on “Hibernate Facts: How to “assert” the SQL statement count

  1. I was struggling with N+1 problem in my project and written an own library for validating the number of queries generated by Hibernate:

    I wasn’t aware about datasource-proxy but still feel that my project is a bit more convenient since it has some synthatic sugar like annotations in JUnit test cases

    I do like your hibernate-master-class project as a playground for learning Hibernate

    1. Congrats for writing the jdbc-sniffer framework. I’ve been using datasource-proxy for logging SQL statements along with bind parameters. It’s also easy to spot the batched SQL statements too. Because it has extension points, I thought of using it for asserting the SQL statement count as well. The hibernate-master-class is indeed a playground for all sorts of Hibernate specific features. I use it as the code base for the High-Performance Hibernate book I’m writing.

Leave a Reply

Fill in your details below or click an icon to log in:

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