Reputation: 190
Spring Data JPA is not a JPA implementation, it is an abstraction of access to database data. I can not understand this expression in the field of comparison with the implementation of JPA (say Hibernate). When using Spring Data JPA, to use CRUD operations, you must extend from the CrudRepository interface. But without Hibernate, Spring Data JPA cannot work independently, since it cannot transform a java object into an Entity. BUT! Hibernate can also perform CRUD operations. So the questions are: 1. Why developers use Spring Data JPA (precisely CRUD operations) if we still need to use HIBERNATE? 2. The CrudRepository interface is just an interface, it is extends from the “Repository” interface. We extend it at our CRUD class to implement CRUD operations. I do not understand how it works. How can our class perform CRUD operations simply by extending the interface. There are no implemented methods. How can I answer these questions for now:
Upvotes: 1
Views: 2372
Reputation: 691625
JPA is a specification which specifies a standard set of classes, methods, annotations, conventions etc. used to map Java Objects to a relational database.
Hibernate is one of the implementations of this specification. It implements all of the stuff defined in the JPA specifications.
Spring Data JPA is a framework which uses JPA, and thus needs an implementation of JPA (Hibernate, or any other one) to be usable. It's a layer of abstraction built on top of the standard JPA specifications.
Why developers use Spring Data JPA (precisely CRUD operations) if we still need to use HIBERNATE?
Because Spring-Data-JPA makes it easy to use JPA from a Spring application, and makes it easier to define repositories: all the basic operations, that you would normally have to write by hand if you didn't use Spring-Data-JPA, are already implemented for you by the framework. Based on conventions and on declarative queries, it implements methods for you, and that makes things faster, safer, and less cumbersome to write.
I do not understand how it works. How can our class perform CRUD operations simply by extending the interface
It uses dynamic proxies: at runtime, it uses reflection to inspect all the additional methods defined in the repository interface, and creates a dynamic proxy, i.e. a class that implements this interface and uses the standard JPA API to implement the methods defined in the interface. It's a relatively advanced topic. Google for "Java dynamic proxy" to get you started. But you can also just use the framework without being able to write it yourself, just like you use a computer without understanding how electronics work.
Upvotes: 9