SpringDataJPA: mapping user data using Native Query

public interface UserRepository extends JpaRepository<User, Long> { @Query(value = "SELECT * FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true) User findByEmailAddress(String emailAddress); } 

Let's say I have the code above where I select * from the user. What if I do not want this method to return a User object. Is there a way to manually map data to a custom MyUser object? Can I do all this in the UserRepository interface?

Thanks!

+6
source share
1 answer

You can do something like this

 @Query(value = "SELECT YOUR Column1, ColumnN FROM USERS WHERE EMAIL_ADDRESS = ?0", nativeQuery = true) List<Object[]> findByEmailAddress(String emailAddress); 

You need to do a mapping. See also the Spring Data Repository. A source

+12
source

All Articles