Close

Spring Data JPA - Dynamic Projections

[Last Updated: Sep 14, 2018]

In Spring Data JPA, projects can also be specified during runtime by using generic repository methods. For example:

public interface EmployeeRepository extends CrudRepository<Employee, Long> {
   <T> List<T> findByDept(String dept, Class<T> type);
}

Example

Entity

@Entity
public class Employee{
  private @Id
  @GeneratedValue
  Long id;
  private String name;
  private String dept;
  private int salary;
  private String phone;
    .............
}

Repository

public interface EmployeeRepository extends CrudRepository<Employee, Long> {

 <T> List<T> findByDept(String dept, Class<T> type);
}

Projection classes

public class SalaryInfo {
  private String name;
  private int salary;

  public SalaryInfo(String name, int salary) {
      this.name = name;
      this.salary = salary;
  }
    .............
}
public class ContactInfo {
  private String name;
  private String phone;

  public ContactInfo(String name, String phone) {
      this.name = name;
      this.phone = phone;
  }
    .............
}

Example client

@Component
public class ExampleClient {

  @Autowired
  private EmployeeRepository repo;

  public void run() {
      List<Employee> employees = createEmployees();
      repo.saveAll(employees);

      System.out.println(" -- finding all employees --");
      Iterable<Employee> all = repo.findAll();
      all.forEach(System.out::println);

      System.out.println(" -- finding SalaryInfo for IT dept --");
      List<SalaryInfo> list = repo.findByDept("IT", SalaryInfo.class);
      list.forEach(System.out::println);

      System.out.println(" -- finding ContactInfo for IT dept --");
      List<ContactInfo> list2 = repo.findByDept("IT", ContactInfo.class);
      list2.forEach(System.out::println);
  }

  private List<Employee> createEmployees() {
      return Arrays.asList(
              Employee.create("Diana", "Admin", 3000, "111-111-111"),
              Employee.create("Mike", "IT", 1000, "222-222-222"),
              Employee.create("Rose", "Admin", 4000, "333-333-333"),
              Employee.create("Sara", "Admin", 3500, "444-444-444"),
              Employee.create("Tanaka", "IT", 3000, "555-555-555"),
              Employee.create("Charlie", "IT", 450, "666-666-666")
      );
  }
}

Main class

public class ExampleMain {

  public static void main(String[] args) {
      AnnotationConfigApplicationContext context =
              new AnnotationConfigApplicationContext(AppConfig.class);
      ExampleClient exampleClient = context.getBean(ExampleClient.class);
      exampleClient.run();
      EntityManagerFactory emf = context.getBean(EntityManagerFactory.class);
      emf.close();
  }
}
 -- finding all employees --
Employee{id=1, name='Diana', dept='Admin', salary=3000}
Employee{id=2, name='Mike', dept='IT', salary=1000}
Employee{id=3, name='Rose', dept='Admin', salary=4000}
Employee{id=4, name='Sara', dept='Admin', salary=3500}
Employee{id=5, name='Tanaka', dept='IT', salary=3000}
Employee{id=6, name='Charlie', dept='IT', salary=450}
-- finding SalaryInfo for IT dept --
SalaryInfo{name='Mike', salary=1000}
SalaryInfo{name='Tanaka', salary=3000}
SalaryInfo{name='Charlie', salary=450}
-- finding ContactInfo for IT dept --
ContactInfo{name='Mike', phone='222-222-222'}
ContactInfo{name='Tanaka', phone='555-555-555'}
ContactInfo{name='Charlie', phone='666-666-666'}

Example Project

Dependencies and Technologies Used:

  • spring-data-jpa 2.0.9.RELEASE: Spring Data module for JPA repositories.
    Uses org.springframework:spring-context version 5.0.8.RELEASE
  • hibernate-core 5.3.6.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • h2 1.4.197: H2 Database Engine.
  • JDK 1.8
  • Maven 3.5.4

Dynamic Projection Example Select All Download
  • spring-data-jpa-dynamic-projections
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • EmployeeRepository.java
          • resources
            • META-INF

    See Also