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.
Post entity looks as follows:
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.
Continue reading “How to use a JVM or database auto-generated UUID identifier with JPA and Hibernate”
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.
Continue reading “How to implement a custom String-based sequence identifier generator with Hibernate”
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 sequence value no longer reflects the current highest in-memory generated value. The database sequence is used as a bucket number, making it difficult for other systems to interoperate with the database table in question. Other applications must know the inner-workings of the hi/lo identifier strategy to properly generate non-clashing identifiers.
The enhanced identifiers
Hibernate offers a new class of identifier generators, addressing many shortcomings of the original ones. The enhanced identifier generators don’t come with a fixed identifier allocation strategy. The optimization strategy is configurable and we can even supply our own optimization implementation. By default Hibernate comes with the following built-in optimizers:
- none: every identifier is fetched from the database, so it’s equivalent to the original sequence generator.
- hi/lo: it uses the hi/lo algorithm and it’s equivalent to the original seqhilo generator.
- pooled: This optimizer uses a hi/lo optimization strategy, but the current in-memory identifiers highest boundary is extracted from an actual database sequence value.
- pooled-lo: It’s similar to the pooled optimizer but the database sequence value is used as the current in-memory lowest boundary
Continue reading “Hibernate pooled and pooled-lo identifier generators”
JPA identifier generators
JPA defines the following identifier strategies:
||The persistence provider picks the most appropriate identifier strategy supported by the underlying database
||Identifiers are assigned by a database IDENTITY column
||The persistence provider uses a database sequence for generating identifiers
||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.
Continue reading “A beginner’s guide to Hibernate enhanced identifier generators”