Let’s see Spring Data Jpa @Query Annotation Example. How to write query methods using @Query annotation.
@Query annotation used with repository methods.
Consider we have an entity called Student.java as below.
package com.javatute.entity; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "name") private String name; @Column(name = "roll_number") private String rollNumber; @Column(name = "university") String university; }
and we have some record in the database as below.
Let’s start with some code snippet which shows how to write a query using Spring Data Jpa @Query.
Writing Native Query using Spring Data Jpa @Query.
@Query(value = "select * from student where name = ?1",nativeQuery = true)
List<Student> getStudents(String name);
Note – For native query, we need to specify nativeQuery = true.
See a complete example from scratch here.
Writing JPQL using Spring Data Jpa @Query.
@Query("select s from Student s where s.name = ?1")
List<Student> getStudents(String name);
See a complete example from scratch here.
Writing the Named Parameter using Spring Data Jpa @Query.
@Query("select s from Student s where s.name = :name")
List<Student> findByName(@Param("name") String name);
See a complete example from scratch here.
Note – The above methods will return the list of student for the corresponding name.
That’s all about Spring Data JPA @Query Annotation.
You may like.
- Spring Data JPA greater than Example
- Spring Data JPA less than Example
- Spring Data JPA IsNull Example Using Spring Boot
- Spring Data findById() Vs getOne()
- Spring Data JPA CrudRepository findById()
- Spring Data JPA JpaRepository getOne()
- Spring Data CrudRepository saveAll() and findAll().
- Spring Data CrudRepository existsById()
- Spring Data JPA delete() vs deleteInBatch()
- Spring Data JPA deleteAll() Vs deleteAllInBatch()
- Spring Data JPA JpaRepository deleteAllInBatch()
- Spring Data JPA deleteInBatch() Example
- Spring Data JPA JpaRepository saveAndFlush() Example
- Spring Data JPA CrudRepository count() Example
- Spring Data JPA CrudRepository delete() and deleteAll()
- Spring Data JPA CrudRepository deleteById() Example
- CrudRepository findAllById() Example Using Spring Boot
- Spring Data CrudRepository save() Method.
- Sorting in Spring Data JPA using Spring Boot.
- Spring Data JPA example using spring boot.
- Spring Data JPA and its benefit.
Other Spring Data JPA and Hibernate tutorials.
- @Version Annotation Example In Hibernate.
- Hibernate Validator Constraints Example Using Spring Boot.
- @Temporal Annotation Example In Hibernate/Jpa Using Spring Boot.
- Hibernate Table Per Concrete Class Spring Boot.
- Hibernate Table Per Subclass Inheritance Spring Boot.
- Hibernate Single Table Inheritance using Spring Boot.
- One To One Mapping Annotation Example in Hibernate/JPA using Spring Boot and Oracle.
- One To One Bidirectional Mapping Example In Hibernate/JPA Using Spring Boot and Oracle.
- One To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And Oracle.
- Many To One Unidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- One To Many Bidirectional Mapping In Hibernate/JPA Annotation Example Using Spring Boot and Oracle.
- Many To Many Mapping Annotation Example In Hibernate/JPA Using Spring Boot And
Spring Data JPA Docs.
Summary – We have seen different ways to define query methods using @Query annotation.