Maven and Java multi-version modules

Introduction In this article, you are going to learn how to configure Maven to choose a specific Java version when the OS has multiple Java versions installed. For instance, FlexyPool uses Java 8 for all modules, except for the flexy-pool-core-java9 module that needs to be built using Java 9.

The Builder pattern and the Spring framework

Introduction In this article, we are going to see how we can use the Builder pattern when creating beans with the Spring framework. I like to make use of the builder pattern whenever an object has both mandatory and optional properties. But building objects is usually the Spring framework responsibility, so let’s see how you can employ it using both Java and XML-based Spring configurations.

Caching best practices

Introduction There is an irresistible attraction to writing custom caching solutions since it seems to be the easiest path to “improving” the overall application performance. Well, caching is a great technique, but there are few steps to consider before even considering it.

The regex that broke a server

Introduction I’ve never thought I would see an unresponsive server due to a bad regex matcher but that’s just happened to one of our services, yielding it unresponsive. Let’s assume we parse some external dealer car info. We are trying to find all those cars with “no air conditioning” among various available input patterns (but without matching patterns such as “mono air conditioning”).

JOOQ Facts: SQL functions made easy

Introduction The JDBC API has always been cumbersome and error-prone and I’ve never been too fond of using it. The first major improvement was brought by the Spring JDBC framework which simply revitalized the JDBC usage with its JdbcTemplate or the SqlFunction classes, to name a few. But Spring JDBC doesn’t address the shortcoming of using string function or input parameters names and this opened the door for type-safe SQL wrappers such as jOOQ. JOOQ is the next major step towards a better JDBC API and ever since I started using it… Read More

How to detect the Hibernate N+1 query problem during testing

Introduction In this article, you are going to learn how to automatically detect the N+1 query problem when using JPA and Hibernate using the Hypersistence Utils open-source project. 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… Read More

Why I like Spring bean aliasing

Spring framework is widely used as a dependency injection container, and that’s for good reasons. First of all, it facilitates integration testing and it gives us the power of customizing bean creation and initialization (e.g. @Autowired for List types). But there is also a very useful feature, that might get overlooked and therefore let’s discuss about bean aliasing. Bean aliasing allows us to override already configured beans and to substitute them with a different object definition. This is most useful when the bean definitions are inherited from an external resource, which is… Read More

JPA Criteria API can generate unexpected SQL statements

Introduction In this article, we are going to learn why you should always check the SQL statements generated by JPA Criteria API queries when using Hibernate. Criteria API is very useful for dynamically building queries, but that’s the only use case where I’d use it. Whenever you have a UI with N filters that may arrive in any M combinations, it makes sense to have an API to construct queries dynamically since concatenating strings is always a path I’m running away from. The question is, are you aware of the SQL queries… Read More

JOOQ Facts: From JPA Annotations to JOOQ Table Mappings

JOOQ is a neat framework, and it addresses a long-time issue I’ve had with advanced dynamic filtered queries. While Hibernate and JPA come with a useful Criteria API, which I’ve been using for quite some time, there are understandable limits to what you can do with those. For instance, you cannot go beyond simple SQL operations (e.g JOINS, NESTED SLECTS, AGGREGATION) and do something like: window functions, user-defined functions or easy sequencing to name a few. JOOQ doesn’t feel like competing with Hibernate, but instead, I feel like it completes it. I’ve… Read More

Hibernate integration testing strategies

Introduction I like integration testing. As I explained in this article, it’s a good way to check what SQL queries are generated by Hibernate behind the scenes. But integration tests require a running database server, and this is the first choice you have to make. Using a production-like local database server for Integration Testing For a production environment, I always prefer using incremental DDL scripts, since I can always know what version is deployed on a given server, and which scripts required to be deployed. I’ve been relying on Flyway to manage… Read More