Skip to main content

JPA-Java Persistence API

JPA-Java Persistence API

The Spring Data Generated DAO – No More DAO Implementations

Spring Data takes this simplification one step further and makes it possible to remove the DAO implementations entirely. The interface of the DAO is now the only artifact that we need to explicitly define.

In order to start leveraging the Spring Data programming model with JPA, a DAO interface needs to extend the JPA specific Repository interface, JpaRepository. This will enable Spring Data to find this interface and automatically create an implementation for it. 

Features 

  1. Simply define a new method in the interface
  2. Provide the actual JPQL query by using the @Query annotation
  3. Use the more advanced Specification and Querydsl support in Spring Data
  4. define custom queries via JPA Named Queries

Composite Primary Keys

  • The composite primary key class must be public.
  • It must have a no-arg constructor.
  • It must define the equals() and hashCode() methods.
  • It must be Serializable.
  1. IdClass Annotation
    public class StockKey implements Serializable {
        private String stockId;
        private String lotId;
        // default constructor
        public AccountId(String stockId, String lotId) {
            this.stockId= stockId;
            this.lotId= lotId;
        }
        // equals() and hashCode()
    }

    @Entity
    @IdClass(StockKey.class)
    public class Stock{
        @Id
        private String stockId;
        @Id
        private String lotId;
        // other fields, getters and setters
    }
  2. EmbeddedId Annotation
    @Embeddable
    public class StockKey implements Serializable {
        private String stockId;
        private String lotId;
        // default constructor
        public AccountId(String stockId, String lotId) {
            this.stockId= stockId;
            this.lotId= lotId;
        }
    }
    @Entity
    public class Book {
        @EmbeddedId
        private StockKey stockId;
        // constructors, other fields, getters and setters
    }


@Query(value = "SELECT e FROM Employee e")

List<Employee> findAllEmployees(Sort sort);


@Query("SELECT e FROM Employee e WHERE e.name = :name and e.salary = :salary")

Employee findEmployeeByNameAndSalaryNamedParameters(  @Param("name") String name,

  @Param("salary") Long salary);


public interface IFooDAO extends JpaRepository<Foo, Long> {

    Foo findByName(String name);

}

@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")

Foo retrieveByName(@Param("name") String name);


@Query(value = "SELECT * FROM Employee e WHERE e.salary = ?1",  nativeQuery = true)

Employee findEmployeeBySalaryNative(Long salary);


PagingAndSortingRepository

public interface StudentDao extends PagingAndSortingRepository<Student, Integer>, JpaRepository<Student, Integer> {

}

Pageable sortedByName =   PageRequest.of(0, 3, Sort.by("name"));

Pageable sortedByPriceDesc =   PageRequest.of(0, 3, Sort.by("price").descending());

Pageable sortedByPriceDescNameAsc = 

PageRequest.of(0, 5, Sort.by("price").descending().and(Sort.by("name")));



Comments

Popular posts from this blog

Spring Boot 4 Ready

Spring Boot 4.x Features Explained:  Java 25, AI Agents, MCP, Resilience, gRPC & Cloud Native Evolution Building on the massive architectural shift introduced in version 3, Spring Boot 4.x bridges the gap between traditional enterprise stability and the rapid demands of modern cloud architecture and AI integration. While Spring Boot 3 introduced the foundations for GraalVM Native Images and Virtual Threads , Spring Boot 4 takes these capabilities mainstream, introduces native resilience patterns, and upgrades the underlying Spring ecosystem. Spring Boot 4 Major Theme: Modern enterprise development with cloud-native architecture, AI integration, improved resilience, better modularity, and optimized JVM performance. Table of Contents 1. Upgraded Foundation & Baselines 2. Built-in Core Resilience Features 3. First-Class Agentic AI & Model Context Protocol (MCP) 4. Native API Versioning Strategy 5. Modern...

Most questions in 2025

Java Spring Boot Senior Engineer Interview Questions & Answers A complete interview preparation guide for Senior Java Engineers, Spring Boot Developers, and Backend Technical Leads covering Java, Spring Boot, Microservices, Cloud, Database, Security, and Performance Engineering concepts. Target Audience: Senior Java Developers Spring Boot Engineers Microservice Architects Backend Technical Leads Table of Contents 1. Describe Yourself 2. RESTful APIs and Standards 3. Latest Spring Boot Version & Features 4. Java Optional 5. WebFlux Reactive Programming 6. WebFlux Retry Mechanism 7. WebFlux Pagination APIs 8. Circuit Breaker 9. Resilience Patterns 10. Rate Limiter 1. Describe Yourself A senior engineer introduction should highlight technical experience, architecture knowledge, ownership, and problem-solving capabilities. Sample Answer I am a Java backend engineer with 7+ years of experience desi...

Data Structure

Java Data Structures A highly-scannable architectural cheat sheet for developers and technical interviews. 1. Linear vs. Non-Linear: The Core Division Data Structure Classification Hierarchy ┌───────────────────────┐ │ Data Structures │ └───────────┬───────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌────────────────────┐ ┌────────────────────┐ │ Linear (Seq) │ │ Non-Linear │ └──────────┬─────────┘ └──────────┬─────────┘ │ │ ┌──────┴──────┐ ┌──────┴──────┐ ▼ ▼ ▼ ▼ [Static] [Dynamic] [Trees] [Graphs] (Array) (Queue, Stack) (BST) (Network) ...