Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a String from JpaRepository Native Query?

I am trying to use a native query to return the value of a certain column within my table.

I know that native queries can be used to return objects, i.e. in this example?:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  User findByEmailAddress(String emailAddress);
}

However, how would I write this to return the value of the one column? I.e. if I wanted to just return the name (string) of the user, not the user object?

like image 242
java123999 Avatar asked Sep 07 '25 09:09

java123999


1 Answers

Try this:

public interface UserRepository extends JpaRepository<User, Long> {

  @Query(value = "SELECT u.name FROM USERS u WHERE EMAIL_ADDRESS = ?1", nativeQuery = true)
  String findByEmailAddress(String emailAddress);
}
like image 176
Jakub Bibro Avatar answered Sep 09 '25 03:09

Jakub Bibro