Is good idea to use Spring JPA entity classes from my data model as entities for domain model in domain driven design? Or should it be mapped into separated ddd entities.

What is best practice / recommended way?

  • 2
    You will find that JPA entities mapped against any ORM makes encapsulation harder. If encapsulation matters to you, you will have to design domain data model and persistence data model apart. For spring your @Entities are mere RowMappers. that's it. – Laiv Jun 14 '18 at 6:56

According to dddsample-core we can have one model for both DDD and ORM. And in my opinion it worth to accept the consequences1 such as this one(Default constructor is required by ORM) and in return we have less duplication.

This may not be related to the question but I feel here is a good place to talk about it. Persistence frameworks abstract models as Entities and they don't usually distinguish them from ValueObjects. But in DDD models we need to clearly see if a model is a ValueObject or Entity? So your 'Spring JPA entity class' must implements ValueObject interface or Entity interface to show this diff. In another words your 'Spring JPA entity calss' might be a ValueObject! and your design should show that clearly.


1: Depends to your chosen technology and frameworks, consequences of such a decision may be different, please take that into account also.

  • aggregates would be exception here ! – Sikorski Sep 14 '18 at 13:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.