What Is the Data Access Object (DAO) Pattern in Java?
A Data Access Object hides how data is stored behind an interface. See a JDBC example, how DAOs differ from repositories, and when Spring Data replaces them.
2,887 posts about Java: questions with answers, tutorials and articles, newest first.
A Data Access Object hides how data is stored behind an interface. See a JDBC example, how DAOs differ from repositories, and when Spring Data replaces them.
The repository pattern hides data access behind a domain-focused interface. Learn its main advantages, a Java example, and when it adds too much ceremony.
Spring Data generates repository implementations from interfaces. Learn derived queries, @Query, projections, paging, custom fragments and how to test them.
The repository pattern puts a collection-like interface between business logic and storage. See plain Java and Spring Data JPA examples.
An EntityGraph controls which associations load with an entity for a single query. See named and dynamic graphs, fetchgraph vs loadgraph and Spring Data JPA usage.
CriteriaBuilder creates type-safe JPA queries, predicates and expressions in Java code instead of JPQL strings. See how to use it for dynamic filters.
Build JPA queries at runtime with CriteriaBuilder, Root and Predicate, add only the filters the user supplied, and keep them type-safe and injection-proof.
The JPA Criteria API builds type-safe, dynamic queries from Java objects instead of strings. See how it works with the metamodel, joins and optional filters.
Speed up JPA writes with Hibernate JDBC batching: configure batch size, use sequence ids, flush and clear per batch, and choose bulk JPQL for uniform updates and deletes.
@Transient marks an entity field as non-persistent so JPA never stores or loads it. See when to use it, with Jakarta Persistence examples.
Define a Java DTO as a record or plain class, map entities to it, validate input and keep request and response shapes separate.
EntityManagerFactory builds JPA metadata once and creates short-lived EntityManager instances. Learn its lifecycle, thread safety and Spring Boot usage.
Optimistic locking in JPA uses a @Version field to detect conflicting updates without database locks. See setup, exception handling and retries.
In JPA, @Transactional scopes the persistence context and the commit or rollback of changes. See dirty checking, lazy loading and read-only use.
Declare repository methods such as findByLastNameAndDepartment and Spring Data JPA builds the query for you. Covers keywords, paging, projections and limits.
@Query overrides derived queries with your own JPQL or native SQL. Learn how Spring Data JPA resolves it, binds parameters, returns projections and handles paging.
Run native SQL in JPA using EntityManager or Spring Data @Query. Covers parameters, DTO and projection mapping, pagination, updates and SQL injection safety.
JPA offers three ways to map a class hierarchy to tables. See code for each, the resulting schema, performance trade-offs and when to pick which.
JPA maps entity hierarchies with @Inheritance and three strategies. See the schema each one creates and how to choose between them.
@Embeddable maps a value class into the table of the entity that owns it. See embedded value objects, column overrides and composite keys with working examples.