Spring Data JPA query method is not same with the Hibernate generated query
09:49 03 Sep 2020

I have a simple Spring Boot application where I just query to a User table. I created a repository IUserRepository where I create the query method that returns specific columns only (spring data projections)

User table

columns (indexes: id, lastName, age)
id
firstName
lastName
age

UserProjection

public interface UserProjection {

     Integer getId();
     String getFirstName();
}

IUserRepository

public interface IUserRepository extends JpaRepository{

     @Query("select u.id, u.firstName from user u where u.lastName = ?1")
     public List findUserByLastName(String lastName);
}

but when I set the hibernate.show_sql=true in my application.properties the result from the hibernate generated query in the logs is

select user0_.id as id_01, user0_.firstName as firstName_01, user0_.lastName as lastName_01, user0_.age as age_01 from user0_ where user0_.lastName = ?

I was expecting to see only the id and firstname since I specified a projection for the return. My expected query should be

select user0_.id as id_01, user0_.firstName as firstName_01 from user0_ where user0_.lastName = ?

Am I doing it wrong? or my understanding is just wrong?

spring-boot hibernate spring-data-jpa