JAVA_CAT
JAVA_CAT

Reputation: 859

JPA Repository Name when uses Projections to select few fields

I have an Entity class Employee

@Entity
@Table(name = "EMP_MASTER")
public class Employee {

    private Integer employeeNumber;
    private String empFirstName;
    private String empSecondName;
    private int age;
    private String empAddress;

//constructor with fields ();
//getters and setters, equals and hascode , toString();
}

Then I have my JPARepo interface. To select employee id and employee Name I am using projections.

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Integer> {

    public List<EmpIdAndEmpNameOnly> find(); // ? nothing is works here.

}

interface EmpIdAndEmpNameOnly {

    int getEmployeeNumber();

    String getEmpFirstName();
}

I am looking to get all the employee number and empfirstname. In this case what will be my repo method name ? tried findEmployee(), findEmployeeNumberAndEmpName(), nothing worked. Any one please help me to find out how this is working ? it gives big confusion all the time for me. Thanks.

Upvotes: 0

Views: 92

Answers (1)

Vikas
Vikas

Reputation: 7185

Simple findAllProjectedBy or findBy() should be fine,

public List<EmpIdAndEmpNameOnly> findAllProjectedBy();

In the case of multiple projections,

List<Projection1> findBy(Projection1.class);
List<Projection2> findBy(Projection2.class);
List<Projection3> findBy(Projection2.class);

Check more details about dynamic projections here

Upvotes: 1

Related Questions