Coffee with Thorben 2020-03-05 – Natural vs Surrogate Keys


Follow me on YouTube to not miss any new videos.

Join the chat on YouTube.

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:

New Course: Spring Data JPA

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.

Unidirectional mapping – composite keys of parent child entities.

This week’s tutorials

Blog

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.

YouTube

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.

2 Ebooks 

to boost your Hibernate skills

Sign up below to join my newsletter and to get your free ebooks:

  • Java 8 Support in Hibernate 5
  • Native Queries with Hibernate

I will collect, use and protect your data in accordance with my Privacy policy.

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.

Related Articles

Responses

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Black Friday & Launch Offer: Join the Persistence Hub!

Hi and welcome!

To make sure I provide you with the best content, please tell me what you are looking for.