简体   繁体   English

Spring boot Jpa:默认休眠?

[英]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?如果使用spring-boot-starter-data-jpa依赖项并通过org.springframework.data.jpa.repository.JpaRepository扩展存储库类,这是“普通 jpa”还是休眠?

What is the difference?有什么不同?

JPA is an interface and Hibernate is the implementation. JPA 是一个接口,Hibernate 是实现。 By default Spring uses Hibernate as the default JPA vendor.默认情况下,Spring 使用 Hibernate 作为默认的 JPA 供应商。 If you prefer, you can use any other reference implementation eg EclipseLink for the Java Persistence in your Spring project.如果您愿意,您可以使用任何其他参考实现,例如 EclipseLink,用于 Spring 项目中的 Java Persistence。

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. Spring Data JPA 旨在通过将工作量减少到实际需要的数量来显着改进数据访问层的实现。 As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.作为开发人员,您编写存储库接口,包括自定义 finder 方法,Spring 将自动提供实现。

Spring Data Jpa acts as high level API and you have to specify what will be the underlying Persistance Provider: Spring Data Jpa 充当高级 API,您必须指定底层持久性提供程序的内容:

1) Eclipse Link Config 1) Eclipse 链接配置

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 2) 休眠配置

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/ )示例基于(基于https://github.com/spring-projects/spring-data-examples/ )中定义的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM