Why you should never use the TABLE identifier generator with JPA and Hibernate

Introduction

From a data access perspective, JPA supports two major types of identifiers:

  • assigned
  • generated

The assigned identifiers must be manually set on every given entity prior to being persisted. For this reason, assigned identifiers are suitable for natural keys.

For synthetic Primary Keys, we need to use a generated entity identifier, which is supported by JPA through the use of the @GeneratedValue annotation.

There are four types of generated identifier strategies which are defined by the GenerationType enumeration:

  • AUTO
  • IDENTITY
  • SEQUENCE
  • TABLE

The AUTO identifier generator strategy chooses one of the other three strategies (IDENTITY, SEQUENCE or TABLE) based on the underlying relational database capabilities.

While IDENTITY maps to an auto incremented column (e.g. IDENTITY in SQL Server or AUTO_INCREMENT in MySQL) and SEQUENCE is used for delegating the identifier generation to a database sequence, the TABLE generator has no direct implementation in relational databases.

This post is going to analyze why the TABLE generator is a poor choice for every enterprise application that cares for performance and scalability.

Continue reading “Why you should never use the TABLE identifier generator with JPA and Hibernate”

How to map calculated properties with JPA and Hibernate @Formula annotation

Introduction

As I explained in this StackOverflow question, mapping calculated properties is very easy with JPA and Hibernate.

In this post, I’m going to demonstrate how you can derive some entity property based on one or multiple persistent entity attributes.

Continue reading “How to map calculated properties with JPA and Hibernate @Formula annotation”

The best way to implement equals, hashCode, and toString with JPA and Hibernate

Bytecode enhancement and toString

Last week, Mark Struberg, who is an Apache Software Foundation member and OpenJPA contributor, made the following statement:

Continue reading “The best way to implement equals, hashCode, and toString with JPA and Hibernate”

How to use external XML mappings files (outside of JAR) with JPA and Hibernate

Introduction

Flemming Harms has asked a very good question on Twitter:

Basically, we want to move the JPA XML mappings outside of the application JAR so that we can change the mapping without affecting the jar file.

Continue reading “How to use external XML mappings files (outside of JAR) with JPA and Hibernate”