Get access to all my video courses, 2 monthly Q&A calls, monthly coding challenges, a community of like-minded developers, and regular expert sessions.
Join the Persistence Hub!
How do you get the primary key values in your application? Do you use natural keys or do you generate technical IDs?
I prefer to generate simple, numerical, technical IDs like you can see in the following code snippet instead of using natural keys which often require the combination of multiple attributes.
@Id private Long id;
Technical IDs are easier to manage and all involved systems, mainly the database and Hibernate, can index them very efficiently. This allows you to focus on the business logic of your application and avoids performance issues.
4 options to generate primary keys
The JPA specification supports 4 different primary key generation strategies which generate the primary key values programmatically or use database features, like auto-incremented columns or sequences. The only thing you have to do is to add the @GeneratedValue annotation to your primary key attribute and choose a generation strategy.
@Id @GeneratedValue private Long id;
The GenerationType.AUTO is the default generation type and lets the persistence provider choose the generation strategy.
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id;
If you use Hibernate as your persistence provider, it selects a generation strategy based on the database specific dialect. For most popular databases, it selects GenerationType.SEQUENCE which I will explain later.
The GenerationType.IDENTITY is the easiest to use but not the best one from a performance point of view. It relies on an auto-incremented database column and lets the database generate a new value with each insert operation. From a database point of view, this is very efficient because the auto-increment columns are highly optimized, and it doesn’t require any additional statements.
@Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id;
This approach has a significant drawback if you use Hibernate. Hibernate requires a primary key value for each managed entity and therefore has to perform the insert statement immediately. This prevents it from using different optimization techniques like JDBC batching.
The GenerationType.SEQUENCE is my preferred way to generate primary key values and uses a database sequence to generate unique values.
It requires additional select statements to get the next value from a database sequence. But this has no performance impact for most applications. And if your application has to persist a huge number of new entities, you can use some Hibernate specific optimizations to reduce the number of statements.
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE) private Long id;
If you don’t provide any additional information, Hibernate will request the next value from its default sequence. You can change that by referencing the name of a @SequenceGenerator in the generator attribute of the @GeneratedValue annotation. The @SequenceGenerator annotation lets you define the name of the generator, the name, and schema of the database sequence and the allocation size of the sequence.
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "book_generator") @SequenceGenerator(name="book_generator", sequenceName = "book_seq", allocationSize=50) private Long id;
The GenerationType.TABLE gets only rarely used nowadays. It simulates a sequence by storing and updating its current value in a database table which requires the use of pessimistic locks which put all transactions into a sequential order. This slows down your application, and you should, therefore, prefer the GenerationType.SEQUENCE, if your database supports sequences, which most popular databases do.
@Id @GeneratedValue(strategy = GenerationType.TABLE) private Long id;
You can use the @TableGenerator annotation in a similar way as the already explained @SequenceGenerator annotation to specify the database table which Hibernate shall use to simulate the sequence.
@Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "book_generator") @TableGenerator(name="book_generator", table="id_generator", schema="bookstore") private Long id;
As you’ve seen, JPA offers 4 different ways to generate primary key values:
- AUTO: Hibernate selects the generation strategy based on the used dialect,
- IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key,
- SEQUENCE: Hibernate requests the primary key value from a database sequence,
- TABLE: Hibernate uses a database table to simulate a sequence.
I prefer to use the GenerationType.SEQUENCE because it is very efficient and allows Hibernate to decide when to perform the insert statement. This provides the required flexibility to use other performance optimization techniques like JDBC batching.
When you like to learn more about performance tuning and how Hibernate can optimize the GenerationType.SEQUENCE, have a look at my Hibernate Performance Tuning Online Training.