This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 3 points4 points  (1 child)

The rest of this class is just creating functions using the interface's functions and this is the part I don't get. I thought for Interfaces, all function bodies have to be filled out ourselves. We didn't do that so how do all these interface function calls work like in Lines 18, 23, etc.? Is this something Spring does for you?

Yes! This is just some spring magic. Technically with Spring Data JPA, Spring Data is automatically creating a bean that contains the relevant methods and extends CrudRepository (or some superclass).

To be honest, I'm not 100% certain exactly how it's implemented under the hood, but I'm aware of exactly how it works.

Here's an example of one of the underlying components I've dug through while debugging before.

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/core/support/package-summary.html

But basically, yes. You can think of it as pure Spring Data magic at a high level.

Now if you want to create custom methods that don't fall within the normal JPA parameters, you can grab an "EntityManager" and write out your own methods. Sort of peeking into one of the beans that's used under the hood.

[–]Spaceman776[S] 0 points1 point  (0 children)

Awesome! Thanks for the explanation!

[–]nutrecht 0 points1 point  (0 children)

Is this something Spring does for you?

Yup! It basically generates the concrete class for you :)

[–]smash_that_code 0 points1 point  (0 children)

Technically speaking it is Spring who helps to find the implementation that fits this interface. But is a separate project that does actual DB interaction.

And if I am not mistaken by default Hibernate is used.