A beginner’s guide to natural and surrogate database keys
Types of primary keys All database tables must have one primary key column. The primary key uniquely identifies a row within a table therefore it’s bound by the following constraints: UNIQUE NOT NULL IMMUTABLE When choosing a primary key we must take into consideration the following aspects: the primary key may be used for joining other tables through a foreign key relationship the primary key usually has an associated default index, so the more compact the data type the less space the index will take the primary key assignment must ensure uniqueness… Read More
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. You only need Hibernate In a real production environment you won’t use Hibernate alone, as you may integrate it in a Java EE 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.
The anatomy of Connection Pooling
Introduction All projects I’ve been working on have used database connection pooling and that’s for very good reasons. Sometimes we might forget why we are employing one design pattern or a particular technology, so it’s worth stepping back and reason on it. Every technology or technological decision has both upsides and downsides, and if you can’t see any drawback you need to wonder what you are missing. The database connection life-cycle Every database read or write operation requires a connection. So let’s see how database connection flow looks like: The flow goes… Read More
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.
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”).
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
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