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.

A beginner’s guide to CDC (Change Data Capture)

Introduction In this article, I’m going to explain what CDC (Change Data Capture) is, and why you should use it to extract database row-level changes. In OLTP (Online Transaction Processing) systems, data is accessed and changed concurrently by multiple transactions, and the database changes from one consistent state to another. An OLTP system always shows the latest state of our data, therefore facilitating the development of front-end applications that require near real-time data consistency guarantees. However, an OLTP system is no island, being just a small part of a larger system that… Read More

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.

The fastest way to update a table row when using Hibernate and Oracle

Introduction Oracle provides several pseudocolumns, and ROWID is one of them. The ROWID pseudocolumn specifies the address of the underlying database record, and according to Oracle documentation, it’s the fastest way to reference a table row. As explained on Ask TOM, there are some operations that might lead to a ROWID change (e.g. partitioning or compacting tables). If that’s the case, then you should not rely on the ROWID pseudocolumn since its value iss no longer consistent. If your database never executes an operation that triggers a ROWID modification, then you should… Read More

Why you should always use hibernate.connection.provider_disables_autocommit for resource-local JPA transactions

Introduction One of my major goals for Hibernate is to make sure we offer all sorts of performance improvements to reduce transaction response time and increase throughput. In Hibernate 5.2.10, we addressed the HHH-11542 Jira issue which allows you now to delay the database connection acquisition for resource-local transactions as well. In this article, I’m going to explain how Hibernate acquires connections and why you want it to delay this process as long as possible.