12
votes

if one uses the spring-boot-starter-data-jpa dependency and extends repository classes by org.springframework.data.jpa.repository.JpaRepository, is this 'plain jpa' or hibernate?

What is the difference?

2
JPA is a specification, Hibernate is an implementation of the specification.Bilal BBB
Spring Data JPA uses Hibernate as a JPA default implementation.Bilal BBB
So there is no such thing as 'plain JPA'? Is hibernate used by default?ynotu.
Isn't the "JPA" in "JpaRepository" a clear enough answer to what API is used? FWIW JpaRepository is not part of the JPA API either, it is Spring's APIuser3973283

2 Answers

24
votes

JPA is an interface and Hibernate is the implementation. By default Spring uses Hibernate as the default JPA vendor. If you prefer, you can use any other reference implementation e.g. EclipseLink for the Java Persistence in your Spring project.

5
votes

From the docs:

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

Spring Data Jpa acts as high level API and you have to specify what will be the underlying Persistance Provider:

1) Eclipse Link Config

Maven

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
        </dependency>

Spring Set-up

@SpringBootApplication
public class Application extends JpaBaseConfiguration {

    protected Application(DataSource dataSource, JpaProperties properties,
            ObjectProvider<JtaTransactionManager> jtaTransactionManagerProvider,
            ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
        super(dataSource, properties, jtaTransactionManagerProvider, transactionManagerCustomizers);
    }


    @Override
    protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
        return new EclipseLinkJpaVendorAdapter();
    }

2) Hibernate Config

Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
        </exclusions>
</dependency>

 <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
</dependency>

Spring Set-up

@SpringBootApplication
class SimpleConfiguration {}

Thats all there is needed to set-up the hibernate provider. Of course you need to define all the key data source properties inside your

src/main/resources/application.properties


spring.datasource.url = jdbc:mysql://localhost:3306/db
spring.datasource.username = root
spring.datasource.password = root
...

Examples are based on projects defined in (based on https://github.com/spring-projects/spring-data-examples/)