Posts

Showing posts from March, 2022

Generate Custom Identifier in Hibernate

Image
Sometimes, we want to generate our table's primary key with more custom information. Custom Identifiers contains more information for us which helps us to distinguish and identify the records. It adds more meaning to an identifier. For example, an event Identifier like - 20220219000045 has two parts, the first part is the date and the second part is the sequence generated number.  YYYYMMDD + 6 digit number generated by the sequence This type of identifier tells us that this event is created on the date: 21/02/2022 . Similarly, we might have different requirements to generate custom Identifiers.  In the post, I will show how can we generate simple sequence based custom identifier in Hibernate. The custom Identifier can be generated in Hibernate by defining our own Identifier generator which implements the Hibernate  IdentifierGenerator interface. However, if you have a database that supports sequences like Oracle RDBMS then you can use SequenceStyleGenerator class in ...