How to write a custom query in Spring Data JPA

Posted By :Saurabh Tiwari |24th February 2020

Assuming you already know the usage of the Spring Data JPA feature of spring framework. Here I am describing how to use the feature of a custom query into Spring Data JPA.

So let's get started.

Suppose you have a Person entity in your project & it has 3 fields id, name, age.)
So here you want to fetch the data by person name then you should use the findByName method to fetch the data of person by name into your repository. 
So I am going to tell you now how to use a custom query in Spring Data JPA. Here steps are given below:-


Step 1 - Create a Person entity/domain inside your entity package.
 
package com.spring.entity;
  public class Person {
  private Integer id;
  private String name;
  private Integer age;
  // setter and getter
  }


Step 2 - Create a PersonService interface inside your service package.

package com.spring.service;
import com.spring.entity.Person;


public interface PersonService {
  List<Person> findPersonsByName(String name);
}


Step 3 - Create a PersonServiceImpl class inside your service impl package.


package com.spring.service.impl;
import com.spring.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PersonServiceImpl implements PersonService {


  @Autowired
  private PersonRepository personRepository;
   
  @Override
  public List<Person> findPersonsByName(String name) {
    return personRepository.findByName(name);
  }


}
Step 4 - Create a PersonRepository interface inside your repository package and extends it with JpaRepository.


package com.spring.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Query;
import com.spring.entity.Person;


@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {
  @Query(value = "SELECT * FROM person WHERE name =:name", nativeQuery = true)
  public List<Person> findPersonsByName(@Param("name") String name);
   
}


Here is a feature of query annotation provided by Spring Data JPA, inside query annotation write you native(custom) SQL query and make sure to use nativeQuery = true to use the feature of a native query.
Use this feature into your Spring MVC project or Spring Boot project.

 


About Author

Saurabh Tiwari

He has good understanding on Java & Spring related technologies. He loves to learn new technologies to update himself.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us