How to use a JVM or database auto-generated UUID identifier with JPA and Hibernate

Introduction In this article, we are going to see how to use a UUID entity identifier that is auto-generated by Hibernate either in the JVM or using database-specific UUID functions. Our Post entity looks as follows: The Post entity has a UUID identifier and a title. Now, let’s see how we can map the Post entity so that the UUID identifier be auto-generated for us.

Why should not use the AUTO JPA GenerationType with MySQL and Hibernate

Introduction As I already mentioned, you should never use the TABLE identifier generator since it does not scale properly. In this post, I’ll show you why you should not rely on the AUTO GenerationType strategy if you’re Hibernate application uses MySQL.

How to implement a custom String-based sequence identifier generator with Hibernate

Introduction One of my blog readers bumped into the assigned generator with a sequence or an identity column post and wondered if it was possible to generate String-based identifiers instead. I accepted the challenge and answered his question on StackOverflow. However, this post is going to explain this topic in greater detail, so there we go.

Hibernate pooled and pooled-lo identifier generators

Introduction In this post, we’ll uncover a sequence identifier generator combining identifier assignment efficiency and interoperability with other external systems (concurrently accessing the underlying database system). Traditionally there have been two sequence identifier strategies to choose from: sequence and seqhilo. The sequence identifier, always hitting the database for every new value assignment. Even with database sequence preallocation, we have a significant database round-trip cost. The seqhilo identifier, using the hilo algorithm. This generator calculates some identifier values in-memory, therefore reducing the database round-trip calls. The problem with this optimization technique is that… Read More

A beginner’s guide to Hibernate enhanced identifier generators

JPA identifier generators JPA defines the following identifier strategies: Strategy Description AUTO The persistence provider picks the most appropriate identifier strategy supported by the underlying database IDENTITY Identifiers are assigned by a database IDENTITY column SEQUENCE The persistence provider uses a database sequence for generating identifiers TABLE The persistence provider uses a separate database table to emulate a sequence object In my previous post I exampled the pros and cons of all these surrogate identifier strategies.

How do Identity, Sequence, and Table (sequence-like) generators work in JPA and Hibernate

Introduction In my previous post I talked about different database identifier strategies. This post will compare the most common surrogate primary key strategies: IDENTITY SEQUENCE TABLE (SEQUENCE) IDENTITY The IDENTITY type (included in the SQL:2003 standard) is supported by: Oracle 12c SQL Server MySQL (AUTO_INCREMENT) DB2 HSQLDB The IDENTITY generator allows an integer/bigint column to be auto-incremented on demand. The increment process happens outside of the current running transaction, so a roll-back may end-up discarding already assigned values (value gaps may happen). The increment process is very efficient since it uses a… Read More

Hibernate and UUID identifiers

Introduction In this article, we are going to see how the UUID entity attributes are persisted when using JPA and Hibernate, for both assigned and auto-generated identifiers. In my previous post I talked about UUID surrogate keys and the use cases when there are more appropriate than the more common auto-incrementing identifiers. A UUID database type There are several ways to represent a 128-bit UUID, and whenever in doubt I like to resort to Stack Exchange for an expert advice. Because table identifiers are usually indexed, the more compact the database type… Read More

A beginner’s guide to natural and surrogate database keys

Types of primary keys All database tables must have one primary key column. The primary key uniquely identifies a row within a table therefore it’s bound by the following constraints: UNIQUE NOT NULL IMMUTABLE When choosing a primary key we must take into consideration the following aspects: the primary key may be used for joining other tables through a foreign key relationship the primary key usually has an associated default index, so the more compact the data type the less space the index will take the primary key assignment must ensure uniqueness… Read More