How can spring data jpa query only some of the fields and not return all of them?

suppose I create a new user with four fields, id, user name, password, and address.
how does spring data jpa query only the username and password fields?
(the actual developed table has more than 20 fields, and it takes 12 seconds for a query to return all fields of a month"s record, but my requirement only needs three fields in the table, and it only takes 0.0083s for a sql query to return a month"s record)

Sep.08,2021

use spring-data 's projection:

public interface UserDto {
  Integer getId();
  String getUsername();
  String getPassword();
}

public interface UserRepository extends JpaRepository<User, Integer> {
  // smart way
  List<UserDto> findBy...(...);

  // 
  @Query("select id, username, password from User where ...")
  List<UserDto> findUserDto(...);
}

  // smart way
  List<UserDto> findBy...(...);

does this method return all fields?

Menu