Spring Data - Query By Example API

Spring Data - Query By Example API

Introduction

Query by Example is a way in Spring that allows for dynamic query creation without writing any code or queries.

Note: In order to use Spring QBE feature, repository class need to be extended from QueryByExampleExecutor interface apart from extending from CrudRepository.

public interface EmployeeRepository extends CrudRepository<Employee, Integer>, QueryByExampleExecutor<Employee> {
	

	    ...

}

Query by Example API

The API mainly gives the following:

a) Example: Example takes a data object (usually the entity object or a subtype of it) and a specification how to match properties

b) Example Matcher: The ExampleMatcher carries details on how to match particular fields. It can be reused across multiple Examples.

Example

Imagine we have an Employee class which has id, name & position field & in order to search all the employees whose position contains the word “Developer” then

 @RequestMapping(value = "/emp/get", method = RequestMethod.GET)
 public ResponseEntity<List<Employee>> EmpgetDetails(

@RequestParam(value="id") int empId,
	                                                  
@RequestParam(value="name") String empName,
	                                                                        @RequestParam(value="position") String position) 
{
	

      Employee emp = new Employee();
      emp.setPosition(position);

      Example<Employee> example = Example.of(emp);
      
      List<Employee> ret_list = empRepository.findAll(example);
      
	  return new ResponseEntity<List<Employee>>(ret_list, HttpStatus.OK);

}

To view or add a comment, sign in

More articles by Gurunath Busetti

  • Sharding Vs Replication

    Sharding When to Use: Large datasets that exceed the capacity of a single database node. High throughput requirements…

  • Best Practices to Ensure Data Integrity and Avoid Dirty Reads

    To mitigate concurrency issues, it is essential to adopt the following best practices in MongoDB: 1. Optimistic Locking…

  • Understanding Kafka Partitions, Message Assignment, and Transactional Data

    Apache Kafka is a leading platform for real-time data streaming, and its ability to handle large volumes of messages…

    1 Comment
  • Spring Boot: Virtual Threads

    Introduction Using Virtual Threads with Spring Boot Virtual threads, introduced in Java 19 through Project Loom, are a…

    1 Comment
  • Best Practices for API Development

    API development is crucial for building robust, scalable, and maintainable systems. Here are some best practices that…

    1 Comment
  • Spring AI: Key Terminologies

    Introduction Spring AI is an innovative application framework tailored for AI engineering. It aims to bring the design…

  • How to create multiple modules in Angular

    Create project using below command $ ng new angularSampleproject The above command generate angular project with…

  • How to update only attributes that have changed - Hibernate

    Question: Hibernate always updates all database columns mapped by my entity, even the ones that I didn’t change. How…

Explore content categories