-1

I am using spring data jpa for my project and i have following pieces of code:

My Repository:

@Repository
@Transactional
public interface StudentRepository extends PagingAndSortingRepository<Student, Long> {

  List<Student> findByIdIn(List<Long> ids);

}

and my entity is :

@Entity
public class Student implements Serializable {

  private static final long serialVersionUID = 1L;

  private Long id;

  // other fields
  // getters/setters

}

Somewhere in my service class, i have

@Autowired
StudentRepository  studentRepository;

and then i call findByIdIn from my service layer like :

studentRepository.findByIdIn(listOfIds);

findByIdIn(listOfIds) method works perfectly fine and everything is working as expected.

I know that the implementation of findByIdIn() method is provided by spring data jpa.

But i am not able to figure where is its implementation present? What exactly is its implementation? Are such methods generated at run time depending upon the method-name? If yes how are they generated and executed dynamically?

Thanks!

user3260035
  • 115
  • 1
  • 5
  • 1
    Please go through this post. It might be helpful :) https://stackoverflow.com/questions/38509882/how-are-spring-data-repositories-actually-implemented?rq=1 – Supun Dharmarathne Jun 08 '18 at 12:30
  • This post explains nicely about the flow when we annotate our additional methods with @ Query. But in this case i have not annotated my method with @ Query. – user3260035 Jun 08 '18 at 13:14

1 Answers1

0

You can dig a little bit in the core of the Spring code to see how it works (https://github.com/spring-projects/spring-data-commons/tree/master/src/main/java/org/springframework/data/repository), but basically, it's parsing the interface methods into HQL at the startup time.

You can test just be editing a method name to a field which doesn't exist, and you'll get an error at startup time saying that there is no field as this.

Sergio Lema
  • 1,491
  • 1
  • 14
  • 25