Home > Software design >  Spring Data JPA vs Hibernate : Where are the implementation of methods in JPARepository?
Spring Data JPA vs Hibernate : Where are the implementation of methods in JPARepository?

Time:07-05

I'm new to JPA and Hibernate. I understand that Hibernate is a implementation of JPA but Spring Data JPA is a JPA Data Access Abstraction (and not an implementation of JPA).

But

StudentRepository.java

@Repository
public interface StudentRepository extends JpaRepository<Student,Integer>{

}

Routing in Controller class

@GetMapping("/all")
    public List<Student> listAllStudents(){
        List listOfStudents = studentRepo.findAll();
        return listOfStudents;
    }

The above code works fine. (Full code and other classes not added to reduce verbosity.) I haven't added Hibernate or any other JPA implementation, I have just used Spring data JPA. But the code works fine (as shown in many tutorials in the web).

This is the dependency of Spring data JPA and I'm using.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.6.7</version>
</dependency>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <scope>runtime</scope>
    </dependency>

What confuses me is if I haven't used any JPA implementation, how does this code work? Where is the implementation of the code?

In logs I could see things related to Hibernate although I haven't added Hibernate dependency or used used anything from Hibernate directly.

2022-07-05 10:55:59.470  INFO 13332 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.9.Final
2022-07-05 10:55:59.729  INFO 13332 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-05 10:55:59.899  INFO 13332 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MariaDB106Dialect

Does it indirectly use Hibernate somehow?

PS: Sorry, if the question is naive, I'm trying to understand how this is working.

Edit : Full code : https://github.com/TMGautham/SpringBootFirst

CodePudding user response:

The spring-boot-starter-data-jpa includes Hibernate. The starters are, as the name implies, starters to make it easier for you to start. So this starter includes

  • Spring Data JPA
  • Hibernate
  • Spring JDBC, Spring Transactions, Spring AOP and Spring Aspects (if needed).

So you automatically get those dependencies you need instead of you trying to cobble together working versions and compatible versions.

So yes you did use an implementation of JPA and by default with the starter it is Hibernate.

  • Related