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
?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With