MariaDB 10.3 supports database sequences

Introduction Traditionally, both MySQL and MariaDB relied on AUTO_INCREMENT columns to generate an IDENTITY Primary Key. Although IDENTITY columns are very efficient in generating the Primary Key value, when it comes to using JPA and Hibernate, the IDENTITY generator prevents us from using JDBC batch inserts. To automatically enroll multiple INSERT, UPDATE or DELETE statements, Hibernate requires delaying the SQL statement until the Persistence Context is flushed. This works very well for the SEQUENCE identifier since the entity identifier can be fetched prior to executing the INSERT statement. However, for IDENTITY columns,… Read More

How to replace the TABLE identifier generator with either SEQUENCE or IDENTITY in a portable way

Introduction As previously explained, the TABLE identifier generator does not scale, so you should avoid id. However, some enterprise applications might need to run on both MySQL (which does not support database sequences), as well as Oracle, PostgreSQL, and SQL Server 2012. This is article is going to explain how easily you can achieve this goal using the JPA mapping overriding.

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.

How to combine the Hibernate assigned generator with a sequence or an identity column

Introduction The entity identifier can either be manually assigned, or it can be automatically generated by an identity column or a database sequence. In this post, I’ll show you how you can mix the assigned generator with an identity column or a database sequence.

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. 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 hi/lo algorithm. This generator calculates some identifier values in-memory, therefore reducing the database round-trip calls. The problem with this optimization technique is that the current database… 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

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