How the JVM will know the implementation of our own methods in the Jpa Repository?

Viewed 65

We can use the Jpa Repository methods like save, delete, etc. Also, we can write our own methods without any implementations by Method Name Conventions. My question is,

Entity class

public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String title;
    private double rating;
    private double watchTime;
    private int releaseYear;
}

Repository class

public interface MovieRepository extends JpaRepository<Movie, Long> {

    List<Movie> findByWatchTimeAndReleaseYear(double 2.30d, int 2020);**//Line i have doubt**
    
}

How the JVM will know the implementation of this method? because we don't implement this.

Can anyone please help me?

1 Answers

It's the Spring Data which uses proxy mechanism at runtime to generate the appropriate instances which are then autowired in appropriate places where you have provided the instance variables for e.g., in your service method. If you debug the code in IDE - you can see all the details of that instance as below

To understand more on how Spring creates and uses runtime generated proxies you can refer to this doc

enter image description here

Related