Search code examples
javahibernatespring-data-jpaspring-data

What are relations between Spring Data, Spring Data JPA and Hibernate?


I'm writing my pet-project with Spring Framework, I have

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

in my pom-file, then

public interface UserJPARepository extends JpaRepository<User, Long>

then

import org.springframework.data.jpa.repository.Query;

...so I never mentioned Hibernate, right?

So why is it when I make a mistake in @Query it's Hibernate reporting the exception?

org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:109)

I've fixed my mistake, but I don't understand why Hibernate is in my project even though I didn't import it anyhow.


Solution

  • Spring Data is a project of the Spring family which provides a unified way to access different kinds of data stores, being that relational databases, NoSQL databases, etc.

    Spring Data includes Spring Data JPA, which provides support for JPA.

    Now, what is JPA (Java Persistence API)?

    Well, it is a specification, and nothing more. This means that JPA is NOT an implementation/provider (you can't use it), but rather a set of rules and definitions on how things should be done.

    To use JPA you need something that implements it. And this is where Hibernate comes into action: Hibernate is an implementation of the JPA specification, so you can work with it.

    Why are you receiving Hibernate messages?

    When you include spring-boot-starter-data-jpa, you are adding several dependencies to your project (it's not a one and only spring-boot-starter-data-jpa dependency), including Hibernate, which is the JPA implementation used by Spring Boot.

    Of course, you could use another JPA provider:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>3.0.0</version> <!-- Make sure to use the latest version -->
    </dependency>
    

    But Hibernate works well.

    So, when you include spring-boot-starter-data-jpa, you are implicitly including Hibernate as well. It is just not visible in the pom.