The @Query annotation supports both JPQL and SQL queries.
The @Query annotation can be used to create queries by using the JPA query language and to bind these queries directly to the methods of your repository interface.
There are three basic types of JPA Queries: Query, written in Java Persistence Query Language (JPQL) syntax. NativeQuery, written in plain SQL syntax. Criteria API Query, constructed programmatically via different methods.
You can use named queries, where the queries have to be defined in a file called META-INF/jpa-named-queries.properties
. See the spring example:
User.findBySpringDataNamedQuery=select u from User u where u.lastname=?1
Reference the query by name in the annotation in your repository, here the corresponding repository example from spring:
@Query(name = "User.findBySpringDataNamedQuery", countProjection = "u.firstname")
Page<User> findByNamedQueryAndCountProjection(String firstname, Pageable page);
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