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 to see what is the best way to map the SINGLE_TABLE inheritance, which, not only is the default inheritance strategy, but it’s usually the most efficient way to model entity inheritance.

Continue reading “The best way to map the SINGLE_TABLE inheritance with JPA and Hibernate”

Advertisements

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.

Continue reading “How to fix “wrong column type encountered” schema-validation errors with JPA and Hibernate”

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 consider using this technique.

In this article, I’m going to show you how you can access a database record by its ROWID when using Hibernate.

Continue reading “The fastest way to update a table row when using Hibernate and Oracle”

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.

Continue reading “Why you should always use hibernate.connection.provider_disables_autocommit for resource-local JPA transactions”