Easily Implement UUIDs As Primary Keys in Spring Boot 3 - Dan Vega
Easily Implement UUIDs As Primary Keys in Spring Boot 3 - Dan Vega
💡 The video tutorial for this blog post can be found above or you can click here to watch it on YouTube.
In this blog post, we will take a trip down memory lane and discuss the evolution of Jakarta EE (formerly
known as Java EE) and its impact on enterprise development in Java. We will explore the role of the
Eclipse Foundation in furthering the development of Jakarta EE and how it has led to the introduction of
new APIs, xes, and features. This article will also delve into one speci c change in Jakarta EE 10, the
ability to use a UUID as a primary key, and how it can be easily implemented in a Spring Boot 3 project
using Spring Data JPA.
WHAT IS JAKARTA EE
Jakarta EE is an open-source platform for developing enterprise-level applications in Java, maintained
and managed by the Eclipse Foundation. It provides secure, scalable, and extensible tools for creating,
deploying, and managing applications. Formerly known as Java EE (Enterprise Edition), it has been
available since 1999. In recent years, Jakarta EE has been updated with new APIs, bug xes, and features,
giving developers more options for creating enterprise applications.
The real question is: why are we discussing this now, and why should Spring developers care? This is
because Spring Framework 6 now has a baseline on Jakarta EE 9 and 10. The two versions are necessary
because we will use 10 wherever possible, and fall back to 9 when we can't. Jakarta EE 9 was the version
where all the namespace changes from javax.* to jakarta.* were made, without any changes to the APIs.
In Jakarta EE 10, we start to see the evolution of the APIs, which includes new features.
Now that you have some background on Jakarta EE and why we are discussing it in the context of a
Spring Application, it's time to write some code. Create a new Spring Boot project and use one of the new
features from Jakarta EE 10.
With our project created, it's time to write some code. Let's start by creating a new class called Product .
When creating an Entity class in Spring Data JPA you need to mark it with the @Entity annotation. Next,
you need to create a new eld called id and annotate it with @Id . Nothing you have done so far is new, but
here is where things get interesting.
You could always annotate the ID eld with @GeneratedValue . This annotation de nes the primary key
generation strategy that the persistence provider must use to generate the primary key. Jakarta EE 10
now adds the GenerationType for a UUID, so that you can use Universally Unique Identi ers (UUIDs) as the
primary key.
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;
private String title;
public Product() {
}
As I mentioned in the video I don’t want to get into the pros/cons of using a UUID as the primary key.
There were a lot of really good comments in the Twitter thread I linked to above so if you’re interested in
that please take a chance to read through that.
Now that you have an Entity in place you need to create a Repository. The Spring Data ListCrudRepository is
an interface that extends the Spring Data Repository interface and adds support for the CRUD (Create,
Read, Update and Delete) operations on a list of objects. It provides basic methods for saving and
retrieving data from a list of objects, and also includes methods for pagination, sorting and querying. This
makes it an ideal choice for applications that require the ability to perform basic CRUD operations on a
list of objects.
Now that you have a repository in place you can create some new products and persist them to the
database. A CommandLineRunner is a Spring Boot interface that is used to execute code when the application
starts up. It is useful for performing any setup tasks that need to be done before the application can be
used. It can also be used to run simple tasks that need to be executed at startup, such as setting up a
database or loading data from an external source. It provides an easy way to execute code when the
application starts, making it a useful tool for developers.
In the following example you will create 3 new products, save them to the database and then print out all
of the records in the db so that you can view the generated IDs.
@SpringBootApplication
public class Application {
@Bean
CommandLineRunner commandLineRunner(ProductRepository productRepository) {
return args -> {
List<Product> products = List.of(new Product("Product 1"),
new Product("Product 2"),
new Product("Product 3"));
productRepository.saveAll(products);
productRepository.findAll().stream().forEach(System.out::println);
};
}
If you run the application you should see the following output:
CONCLUSION
The transition to Jakarta EE 9/10 was not easy, but it was necessary for the Spring Framework. Going
forward, the Spring Framework and its associated libraries can bene t from the evolution of the Jakarta
EE APIs.
spring boot
Follow me on Twitter, LinkedIn, or sign up for my newsletter to get my latest articles and tutorials.
DAN VEGA