Distinction between natural and surrogate identifiers #109
Unanswered
martinleim
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Considering the following (synthetic) scenario:
My domain is about managing books. Each book is uniquely identified by a ISBN. Behind the scenes, I want to store the books in a SQL database into an existing schema with an auto-incremented numeric primary key. The callers of that domain should not know nothing about the database, they should only identify books based on the ISBN.
I am now trying to model this with the DDD types.
Book
looks roughly like this:I could create a distinct JPA entity which has a numeric
@Id
, with the isbn as a unique constraint. However, this requires a lot of boilerplate code. I am aware ofjmolescules-bytebuddy
from the jmolecules-integration. Unfortunately, I cannot use this since it assumes that the identifier of theAggregateRoot
is theId
of the JPA entity.In my opinion, using natural keys (and omitting surrogate ones) in domain models and using auto-incremented primary keys in JPA models are both good practices. Would it be conceivable that jModulecules could support something like this? For instance, there could be an additional annotation to control the JPA boilerplate creation. Tools like
jmolescules-bytebuddy
could pick that up to create an additional getter with the@Id
annotation.Beta Was this translation helpful? Give feedback.
All reactions