Sobes.tech
Junior — Middle

Is it possible to integrate native SQL queries when working with Spring Data JPA?

sobes.tech AI

Answer from AI

Yes, in Spring Data JPA, you can use native SQL queries. To do this, you can declare a method in the repository with the @Query annotation, specifying the parameter nativeQuery = true.

Example:

public interface UserRepository extends JpaRepository<User, Long> {
    @Query(value = "SELECT * FROM users WHERE email = ?1", nativeQuery = true)
    User findByEmailNative(String email);
}

Thus, you can execute any SQL queries if the standard JPA methods are insufficient or if optimization is needed.