r/SpringBoot • • Oct 27 '25

How-To/Tutorial Spring Data JPA Best Practices: Entity Design Guide

https://protsenko.dev/spring-data-jpa-best-practices-entity-design-guide/

Hi everyone, I've written the comprehensive article on designing Spring Data JPA entities, it's the quintessence of my 9 years of experience with this technology. These best practices could help you save your codebase from legacy code and avoid common mistakes.

I will publish two more guides soon because the original felt more like a mini-book than an article.

Your feedback is very welcome to me. I hope you find this article helpful.

50 Upvotes

7 comments sorted by

3

u/onated2 Oct 28 '25

.What do you think about creating an abstract Entity?

3

u/NordCoderd Oct 28 '25

It depends on your needs, but having some base class marked @MappedSuperClass it totally ok. Sometimes entity fields could be common like createdAt or updatedAt, and there is no need to declare them in each entity.

1

u/NordCoderd Nov 05 '25

The second article in my series on Spring Data JPA. Today I covered many aspects of Spring Data JPA repositories development, I hope you find it helpful.

https://protsenko.dev/spring-data-jpa-best-practices-repositories-design-guide/

2

u/[deleted] Oct 29 '25

[removed] — view removed comment

1

u/NordCoderd Oct 30 '25

> Would you then agree with the generalization, that entities should have 0 domain logic?

From my perspective, yes, but there could be nuances with simple CRUD operations where you could use only implemented methods from JPA repositories to build logic. In that case, it's acceptable to have logic in the service layer that operates with entities.

Actually, it depends on the project and your preferences, but returning DTO will reduce the problems you mentioned and keep the codebase without unexpected behavior due to knowledge gaps. Spring Data JPA/Hibernate has many nuances, and I encountered numerous problems due to not being familiar with them.

> At the end of the day, it is always a skill issue I suppose

I don't think you have skill issues, as you explain your deep thoughts on the subject. At least JPA is just a framework, and we need to choose things that are suitable for us.