How do you implement validation in JPA entities?
Annotate JPA entities with Jakarta Bean Validation constraints, learn when Hibernate validates them, and combine them with database constraints.
2,886 posts about Java: questions with answers, tutorials and articles, newest first.
Annotate JPA entities with Jakarta Bean Validation constraints, learn when Hibernate validates them, and combine them with database constraints.
@EnableJpaRepositories registers Spring Data JPA repositories. See when Boot auto-configures it and how to scan custom packages or data sources.
Set the datasource under spring.datasource, JPA behaviour under spring.jpa and Hibernate settings under spring.jpa.properties. Includes safe production defaults.
The spring-boot-starter-data-jpa dependency bundles Spring Data JPA, Hibernate and transactions, and auto-configures them from your database properties.
Step-by-step Spring Boot 3 guide to JPA: dependencies, datasource settings, entities, repositories, services, REST endpoints, and testing.
Spring Data repository methods are transactional by default. Learn when to override with @Transactional for modifying queries, propagation and timeouts.
Add custom methods to Spring Data repositories using fragment interfaces and Impl classes, or a shared base repository class for every repository.
The EntityManager is the core JPA interface: it manages entities in a persistence context and runs persist, find, merge, remove and queries within transactions.
Learn how @Modifying and @Query run bulk UPDATE and DELETE statements in Spring Data JPA, and how to avoid stale entities and missing transactions.
Learn how to create and use PageRequest in Spring Data: the factory methods, sorting, navigation and the SQL it produces.
Build paginated queries with Spring Data JPA from entity to controller and test, including Page, Slice and keyset scrolling.
Write Spring Data JPA query methods: derived names, JPQL and native @Query, Pageable, projections, @Modifying updates and @DataJpaTest tests.
@Version adds an optimistic lock counter to a JPA entity so concurrent updates are detected instead of overwritten. See the SQL, a conflict demo and handling.
Use @Version to enable optimistic locking in JPA. See how version checks prevent lost updates, how to handle conflicts, and which lock modes to use.
@JoinTable sets the name, foreign key columns and constraints of the join table behind a @ManyToMany or unidirectional @OneToMany mapping in JPA.
Define a JPA many-to-many relationship with @ManyToMany, @JoinTable and mappedBy, and learn when a join entity is the better design.
@ManyToOne maps many entities to one parent through a foreign key. See a working Employee and Department example, lazy fetching and how to avoid N+1 queries.
@OneToMany maps a parent's collection of child entities. See bidirectional and unidirectional mappings, cascade rules and how to avoid N+1 queries.
CacheManager maps cache names to providers in Spring Boot. See how auto-configuration picks one, how to configure TTL, and how to evict entries.
Enable caching with @EnableCaching, annotate service methods with @Cacheable and switch the default map to Caffeine for size limits and expiry.