Articles

Affichage des articles du octobre, 2022

Manipulating Entity ID before it gets inserted

Introduction In this tutorial I will explain how to manipulate an entity  ID  even before it gets inserted into the database table. For that purpose I will use spring framework (spring -boot) and hibernate. Use case Assume that you have a column that you need to update based on the row  ID ; however the entity is not created yet; so the  ID  doesn't exist yet. A basic use case consists of creating an entity  Odd , that has besides its  ID  a property called  isOdd  to store whether the ID is odd or even. GitHub repository A working spring-boot project is available on this  GitHub repository Let's get hands dirty Use a sequence generator for the ID The first step is delegating the  ID  generation to Hibernate by using a sequence generation strategy and by defining a sequence generator for it, that way the  ID  will be generated and included in the insert query; that hibernate will send to the RDBMS for persistence...