r/SpringBoot • • Feb 02 '26
Question Spring Boot + JPA: validation annotations on entities or only on DTOs?
Hello guys, I’ve been reading a lot of posts and discussions about validation in Spring Boot with JPA, and I keep finding two opposing recommendations, so I’d like to get some clarity from people with real-world experience.
On one side, many posts suggest using validation annotations directly on JPA entities, arguing that JPA/Hibernate can pick up some of these constraints. It helps generate the database schema correctly.
On the other side, many people strongly recommend not using validation annotations on entities at all, and instead, keep entities “pure” and focused on persistence, Put all validation only in DTOs.
A little example that comes to mind and i've seen on some posts is this:

I know the purpose of each (Column nullable is for SQL rules, notnull is for validation before SQL)
Also, i'm using Flyway and my current aproach is: write migrations by hand (SQL), and apply them in dev and prod environment (for the dev environment Postgres instance i use a docker compose file, before starting the Spring Boot App).
Any downsides to always using Flyway, even locally? Should i let Hibernate generate the schema in dev and only use Flyway in staging/prod?
Thank you!
View all comments
Show parent comment
1
u/twhickey Feb 02 '26
Why not both? I view the business logic validations at the DTO level as protecting the user, and validation at the Entity / DB level as protecting the system. Somewhat parallel to rejecting invalid input both in the UI (for responsiveness and feedback to the user) and in the business logic, to protect the system.
Edit: In case it wasn't clear, I 100000% agree that ORM Entities should never escape the persistence layer. And (if you're in Java), the code structure should be such that you can write ArchUnit tests that enforce no access to entities outside of the service that owns them.