Mapping PostgreSQL Interval to Java Duration with Hibernate

Introduction In this article, we are going to see how to map the PostgreSQL interval column type to a Java Duration object using Hibernate and the hibernate-types project. Another very useful feature introduced by the hibernate-types project is that all types extending the ImmutableType can now be treated as standard org.hibernate.type.Type, therefore enabling a much better Hibernate Core API integration.

How to map Java and SQL arrays with JPA and Hibernate

Introduction Hibernate custom Types allow you to map all sorts of database specific column types, like IP address, JSON columns, bit sets or SQL arrays. There are two ways to define a custom Hibernate Type: the UserType interface Java and SQL descriptors The latter option is preferred since it allows you to better split the Java-to-JDBC and the JDBC-to-SQL type handling. In this article, we are going to see how you can map SQL arrays to their Java counterpart.

How does MySQL result set streaming perform vs fetching the whole JDBC ResultSet at once

Introduction I read a very interesting article by KreŇ°imir Nesek regarding MySQL result set streaming when it comes to reducing memory usage. Mark Paluch, from Spring Data, asked if we could turn the MySQL result set streaming by default whenever we are using Query#stream or Query#scroll. That being said, the HHH-11260 issue was created, and I started working on it. During Peer Review, Steve Ebersole (Hibernate ORM team leader) and Sanne Grinovero (Hibernate Search Team Leader) expressed their concerns regarding making such a change. First of all, the MySQL result set streaming… Read More

The best way to map a Java 1.8 Optional entity attribute with JPA and Hibernate

Introduction StackOverflow is a neverending source of great questions. This time, we are covering this question about using Java 1.8 Optional with JPA and Hibernate. Java 1.8 introduced the java.util.Optional container object that may or may not contain a certain value. Combining Optional and streams is very handy. Therefore, you might want that some nullable entity attributes be exposed as Optional. This article is going to demonstrate what are caveats of using Optional with entity attributes, and how you can overcome them.

How to enable bytecode enhancement dirty checking in Hibernate

Introduction Hibernate runs the automatic dirty checking mechanism during flush-time, and any managed entity state change is translated into an UPDATE SQL statement. The default dirty checking mechanism uses Java reflection and goes through every property of every managed entity. If the Persistence Context has few entities, this process might go unnoticed, but, if we’re dealing with many entities or the entities have many properties (e.g. a legacy database Domain Model mapping), then the reflection-based dirty checking might have an associated performance impact.

How does FlexyPool support both Connection proxies and decorators

Proxies FlexyPool monitors connection pool usage and so it needs to intercept the connection close method call. For simplicity sake, the first version was relying on dynamic proxies for this purpose: As straightforward as it may be, a proxy invocation is slower than a decorator, which calls the target method using a direct invocation. Because all connection pools use proxies anyway, adding another proxy layer only adds more call-time overhead and so now FlexyPool supports connection decorators as well.

How to monitor a Java EE DataSource

Introduction FlexyPool is an open-source framework that can monitor a DataSource connection usage. This tool come out of necessity, since we previously lacked support for provisioning connection pools. FlexyPool was initially designed for stand-alone environments and the DataSource proxy configuration was done programmatically. Using Spring bean aliases, we could even substitute an already configured DataSource with the FlexyPool Metrics-aware proxy alternative.

How to prevent OptimisticLockException with Hibernate versionless optimistic locking

Introduction In my previous post I demonstrated how you can scale optimistic locking through write-concerns splitting. Version-less optimistic locking is one lesser-known Hibernate feature. In this post, I’ll explain both the good and the bad parts of this approach. Version-less optimistic locking Optimistic locking is commonly associated with a logical or physical clocking sequence, for both performance and consistency reasons. The clocking sequence points to an absolute entity state version for all entity state transitions. To support legacy database schema optimistic locking, Hibernate added a version-less concurrency control mechanism. To enable this… Read More

Spring request-level memoization

Introduction Memoization is a method-level caching technique for speeding-up consecutive invocations. This post will demonstrate how you can achieve request-level repeatable reads for any data source, using Spring AOP only. Spring Caching Spring offers a very useful caching abstracting, allowing you do decouple the application logic from the caching implementation details. Spring Caching uses an application-level scope, so for a request-only memoization we need to take a DIY approach.

Maven and Java multi-version modules

Introduction Usually, a project has a minimum Java version requirement and that applies to all of its modules. But every rule has its exceptions, as recently I stumbled on the following issue. One open source project of mine mandates Java 1.6 for most of its modules, except one requiring the 1.7 version. This happens when integrating external libraries having different Java requirements than your own project. Because that one module integrates the DBCP2 framework (supporting at least Java 1.7), I need to instruct Maven to use two different Java compilers.