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?
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.
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.
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.
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.
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/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With