Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot Jpa: hibernate as default?

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?

like image 239
ynotu. Avatar asked Jan 27 '17 20:01

ynotu.


People also ask

Can we use Spring data JPA with Hibernate?

Hibernate is the default JPA implementation used by Spring Data JPA. NOTE: Java 17 and Spring Boot 3 are required for this course. JPA stands for Java Persistence API. This is a common Java API used to work with Relational Databases.

Is Spring data JPA same as Hibernate?

JPA is a specification and defines the way to manage relational database data using java objects. Hibernate is an implementation of JPA. It is an ORM tool to persist java objects into the relational databases. JPA uses javax.

Is the starter for using Spring data JPA with Hibernate in Spring boot applications?

Spring Boot provides spring-boot-starter-data-jpa dependency to connect Spring application with relational database efficiently. The spring-boot-starter-data-jpa internally uses the spring-boot-jpa dependency (since Spring Boot version 1.5. 3). The databases are designed with tables/relations.


2 Answers

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.

like image 86
fabfas Avatar answered Oct 12 '22 23:10

fabfas


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/)

like image 27
Maciej Kowalski Avatar answered Oct 12 '22 23:10

Maciej Kowalski