How to store schema-less EAV (Entity-Attribute-Value) data using JSON and Hibernate
Introduction One of my Twitter followers has recently asked me about a way of storing EAV (Entity-Attribute-Value) data using JPA and Hibernate, and, because this is a very good question, I decided to turn into a blog post. In this article, you are going to see how you can use JSON types to store schema-less EAV data.
How to optimize the merge operation using update while batching with JPA and Hibernate
Introduction One of my readers has recently asked me about optimizing the merge entity state transition, and, because this is a great question, I decided to turn it into a blog post. In this article, you are going to see a shortcoming of the merge entity state transition and how you can deal with it using Hibernate.
What’s new in JPA 2.2 – Stream the result of a Query execution
Introduction In this article, we are going to see how the JPA 2.2 Stream query results are supported by Hibernate and the caveats of using database cursors just to limit the amount of data that needs to be fetched.
What’s new in JPA 2.2 – Java 8 Date and Time Types
Introduction In this article, we are going to see how JPA 2.2 Date/Time works, and which types you need to use depending on your business case requirements.
How to map Java and SQL arrays with JPA and Hibernate
Introduction In this article, we are going to see how you can map SQL arrays to JPA entity attributes when using Hibernate. Hibernate custom Types allow you to map all sorts of database-specific column types, like IP address, JSON columns, bit sets, or SQL arrays. However, while you can create your own custom Hibernate Types for mapping PostgreSQL arrays, you don’t need to implement your own Hibernate Type. All you need to do is use the Hypersistence Utils open-source project.
The best way to map the @DiscriminatorColumn with JPA and Hibernate
Introduction As previously explained, the SINGLE_TABLE inheritance is the most efficient entity inheritance strategy. However, for JPQL query such as this one: Hibernate generates a SQL query which filters by the associated discriminator column (e.g. DTYPE by default): So, because we are filtering by the discriminator column, we might want to index it or include it to speed up queries. However, the default STRING DiscriminatorType expects a VARCHAR column that must hold the longest entity subclass name. For the Announcement class, we need at least 12 bytes to store the entity class… Read More
The best way to use entity inheritance with JPA and Hibernate
Introduction Recently, my friend Lukas Eder wrote the following message on Twitter: Just like in any OOP (Object-Oriented Programming) language, entity inheritance is suitable for varying behavior rather than reusing data structures, for which we could use composition. The Domain Model compromising both data (e.g. persisted entities) and behavior (business logic), we can still make use of inheritance for implementing a behavioral software design pattern. In this article, I’m going to demonstrate how to use JPA inheritance as a means to implement the Strategy design pattern.
The best way to map the SINGLE_TABLE inheritance with JPA and Hibernate
Introduction Java, like any other object-oriented programming language, makes heavy use of inheritance and polymorphism. Inheritance allows defining class hierarchies that offer different implementations of a common interface. Conceptually, the Domain Model defines both data (e.g. persisted entities) and behavior (business logic). Nevertheless, inheritance is more useful for varying behavior rather than reusing data (composition is much more suitable for sharing structures). Even if the data (persisted entities) and the business logic (transactional services) are decoupled, inheritance can still help varying business logic (e.g. Visitor pattern). In this article, we are going… Read More
How to fix “wrong column type encountered” schema-validation errors with JPA and Hibernate
Introduction Mapping entities to database tables is usually a very straightforward process. However, if your mappings are rather unusual, you might bump into some rare issues like this one I found on the Hibernate forum. In this article, I’m going to explain the mapping between Java objects to JDBC and database column types, and how you can fix the issue described in the aforementioned Hibernate question.

