Skip to main content

Native Query with @Query

Learn Native Query with @Query (Spring Data JPA, Spring Boot) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.

How to Write a Native Query • Use the @Query annotation to specify a query inside a repository. • Always write the SQL query in the repository layer only - never in the service or the controller. • You can refer to the table name or the POJO class name while fetching the data, depending on which kind of query you write. The one attribute that changes everything Correction worth noting: writing nativeQuery = false with SQL in the string does not produce a compile-time error - @Query takes a plain String, so the compiler has nothing to check. It fails when Hibernate tries to parse it as JPQL,...

More lessons in Spring Data JPA