# Spring Data JPA Derived Query methods with Example

When using Spring Data JPA, developers can use derived query methods to generate queries based on the name of the method. This approach eliminates the need for manually writing queries, reducing boilerplate code and increasing productivity.

Derived queries use a simple naming convention based on the method name, so developers can easily write them without having to learn a new query language. In this article, we'll explore how to use Spring Data JPA-derived query methods and provide examples of how they work.

## **Prerequisites**

To follow this article, you'll need the following:

* Java 11 or later
    
* Spring Boot 2.5 or later
    
* Spring Data JPA
    
* An IDE (e.g. IntelliJ IDEA or Eclipse)
    

## **Setup**

Create a new Spring Boot project using your favorite IDE. Make sure to include the Spring Data JPA dependency in your `pom.xml` file:

```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
```

## **Derived Query Methods**

Derived query methods use a naming convention that maps the method name to the query that should be generated. The basic syntax is as follows:

```java
find[Entity]By[Property]
```

Here, `[Entity]` refers to the name of the entity you want to query, and `[Property]` is the name of a property of that entity. For example, if you have an entity called `User` with a property called `email`, you can generate a query to find all users with a specific email address like this:

```java
List<User> findByEmail(String email);
```

This method will generate a query that looks like this:

```sql
SELECT * FROM users WHERE email = :email
```

If you want to query based on multiple properties, simply add more properties to the method name:

```java
List<User> findByEmailAndFirstName(String email, String firstName);
```

This method will generate a query that looks like this:

```sql
SELECT * FROM users WHERE email = :email AND first_name = :firstName
```

You can also use operators like `Like` and `Between` in your method names:

```java
List<User> findByEmailLike(String email);
List<User> findByCreatedDateBetween(LocalDateTime start, LocalDateTime end);
```

These methods will generate queries like this:

```sql
SELECT * FROM users WHERE email LIKE %:email%
SELECT * FROM users WHERE created_date BETWEEN :start AND :end
```

You can also use keywords like `OrderBy` and `Top` to control the order and number of results:

```java
List<User> findByOrderByEmailAsc();
List<User> findTop10ByOrderByCreatedDateDesc();
```

These methods will generate queries like this:

```sql
SELECT * FROM users ORDER BY email ASC
SELECT * FROM users ORDER BY created_date DESC LIMIT 10
```

## **Conclusion**

Spring Data JPA-derived query methods provide a simple and convenient way to generate queries without having to write SQL statements manually. By following a simple naming convention, developers can quickly generate complex queries that would otherwise require a lot of boilerplate code.

## **References**

* [**Spring Data JPA - Query Creation**](https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)
    

I hope this helps, you!!

**More such articles:**

[https://medium.com/techwasti](Link)

[https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA](Link)

[https://www.techwasti.com/](Link)

\==========================\*\*=========================

**If this article adds any value to you then please clap and comment.**

**Let’s connect on** [**Stackoverflow**](http://stackoverflow.com/users/3187349/maheshwar-ligade)**,** [**LinkedIn**](https://in.linkedin.com/in/maheshwar-ligade-14447841)**, &** [**Twitter**](https://twitter.com/MaheshwarLigade)**.**
