From the course: Spring Data

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

@Query annotation

@Query annotation - Spring Tutorial

From the course: Spring Data

@Query annotation

- [Instructor] We've seen some pretty cool ways to make query methods using property expressions in the signature method. Another way is to declare a query method by applying the @Query annotation on that method. The contents of the @Query annotation can be a source specific query language, like JPQL, or query language native to the actual data source management system, in this case H2 SQL. When declaring a query method with @Query, the method signature can be anything you like. It does not have to follow property expression rules. Let's look at some reasons where @Query is the better choice. It is encouraged to use non-native queries because they are verified at Bootstrap. Native queries are not verified until they're invoked. So why would we want to use @Query method simplification? Sometimes property expressions can get too long and awkward. Doesn't findByChairLastName seem more natural than findByDepartmentChairMemeberLastName? Parameter substitution is possible with the @Param…

Contents