Natural vs Surrogate Primary Keys
When modeling your entity classes and database tables, you need to decide what kind of primary key you want to use. You can use a combination of attributes that identify an object. This is called a natural key. Or you could introduce an artificial, technical key, which is called a surrogate key.
In this stream, we will discuss how to best map these keys and when you should use which approach.
New Course: Spring Data JPA
If you enjoyed the live stream, you will love my online courses. At the end of this month, I will offer my new Spring Data JPA course for the first time. Make sure to join my email list to get updates about the course and not miss my special launch offer:
Question of the Week
This week’s question is also related to primary key mappings. Gurkirat reached out to me on LinkedIn and asked if I could help him with a mapping problem he posted on StackOverflow.
He has a parent entity that uses a composite primary key and one part of it is generated by a database sequence. On the child entity, he wants to use that generated attribute as a part of the primary key.
This week’s tutorials
Since the introduction of Spring Boot, configuring Spring Data JPA has become incredibly simple. Spring Boot automatically handles most of the configuration and integrations for you. But you should still know which default values it uses and how to adjust them: Configuring Spring Data JPA with Spring Boot.
Hibernate gets often introduced to a project to implement UI-driven CRUD operations. And after that worked well, that application might need another batch job, which then also gets implemented with Hibernate.
Unfortunately, Hibernate isn’t a great fit for that. Like most object-relational mapping frameworks, it maps each database record to an entity object. It processes each one as an independent element with its own lifecycle and its own database operations. This creates an overhead if you want to implement a batch job that works on thousands of records.
That’s why using a different persistence framework is often the easiest solution. But if you can’t do that, there are a few things you can do to improve Hibernate’s performance in batch jobs: Implementing Batch Jobs with Hibernate.