Enums, Enums, Enums …
Enums are the perfect data type, if an attribute can only have one out of a set of predefined values. A typical example is the rating of a product, which can have the values 1, 2, 3, 4 and 5.
Using JPA’s default mappings, you can persist an enum value as a String or as its ordinal value. In addition to that, we can provide our own mappings using JPA’s standard types or implement a custom type that supports database-specific enum types.
In today’s stream, we will take a look at all 4 options and discuss when you should use which of them. If you want to dive deeper into this topic, I recommend the following articles:
- Enum Mappings with Hibernate – The Complete Guide
- JPA 2.1 Attribute Converter – The better way to persist enums
Live Online Workshops
If you enjoyed the live stream, you will love my live online workshops. Find out more at thorben-janssen.com/workshops.
Question of the Week
Ajay asked this week’s question of the week in the comments of one of my recent YouTube videos about DTO projections. He wanted to know if there is any support for nested DTO projections.
This week’s tutorials
Refactoring the persistence layer is the most critical part when splitting a monolith into microservices. You need to make the services as independent as possible while also ensuring data consistency and providing great performance.
In this week’s post, I explained the general steps to split up a monolith, some of the patterns you should know to solve common problems, and when you should rethink your design decisions.
Find out more at From Monolith to Microservices – Migrating a Persistence Layer.
Using JPA and Hibernate, you can map a Collection in various ways. One option is to map them as an association to another entity class. Or, if you don’t want to model another class, you can use an @ElementCollection or as your own basic type.
I explain all 3 options with their advantages and disadvantages in this week’s video: