【问题标题】:Whether Spring boot Data JPA uses hibernate internally for it's implementation?Spring boot Data JPA 是否在内部使用休眠来实现它?
【发布时间】:2018-12-05 07:49:49
【问题描述】:

我正在建立一个现有的项目,我在 pom.xml 中找到了以下依赖项

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

但是hibernate没有任何依赖,所以如何检查一个项目是否使用了hibernate。

我还发现了包含以下代码的 JpaConfiguration 类。

@Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);

        String entities = ClassUtils.getPackageName(ArkonnApplication.class);
        String converters = ClassUtils.getPackageName(Jsr310JpaConverters.class);
        entityManagerFactoryBean.setPackagesToScan(new String[]{entities, converters});

        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", dialect);
        jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddlAuto);
        jpaProperties.put("hibernate.show_sql", showSql);
        jpaProperties.put("hibernate.format_sql", formatSql);
        jpaProperties.put("hibernate.use_sql_comments", useSqlComments);
        entityManagerFactoryBean.setJpaProperties(jpaProperties);

        return entityManagerFactoryBean;
    }

如果这个项目正在使用休眠,那么为什么它没有任何依赖或配置。

1) Spirng boot Data JPA 是否带有默认的 Hibernate 配置?.

2) 如果是,Spring Boot Data JPA 如何在内部与 Hibernate 一起工作。(任何参考链接)

【问题讨论】:

  • mvn dependency:tree 将解决您项目的所有依赖项。在那里你应该可以看到hibernate 依赖

标签: spring hibernate jpa spring-data-jpa


【解决方案1】:

JPA 是一个接口,而 Hibernate 是实现。默认情况下,Spring 使用 Hibernate 作为默认的 JPA 供应商。您可以在 pom.xml 下的依赖层次结构中看到 hibernate 相关的依赖关系,这将解决您项目的所有依赖关系。

【讨论】:

    【解决方案2】:

    spring-boot-starter-data-jpa 默认带有hibernate。在我上面的评论中,从项目的工作区文件夹中打开一个终端/cmd 并发出:

    mvn dependency:tree

    这将解决您项目的所有依赖项。在那里你应该能够看到休眠依赖。

    同样来自依赖部分的 spring-boot-starter-data-jpa pom.xml

    <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>org.jboss.spec.javax.transaction</groupId>
                        <artifactId>jboss-transaction-api_1.2_spec</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    

    参考:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-starters/spring-boot-starter-data-jpa/pom.xml

    【讨论】:

      【解决方案3】:

      如果您使用的是 Spring Boot,则应将 jpa 属性添加到 application.yml,如下所示:

      spring:
        datasource:
          #url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
          url: jdbc:h2:~/nexin;DB_CLOSE_DELAY=-1;MODE=MySQL;MV_STORE=FALSE;MVCC=FALSE
          username: nexin
          password:
          driver-class-name: org.h2.Driver
          platform: h2
        jpa:
          hibernate:
            ddl-auto: create
            show_sql: true
        h2:
          console:
            enabled: true
            path: /console
        logging:
          config: classpath:logback.xml
      

      如果你在你的项目上做一个 mvn dependency:tree 来查看 spring jpa 引入的依赖项,你会看到 hibernate 在那里:

       +- org.springframework.boot:spring-boot-starter-data-jpa:jar:2.0.3.RELEASE:compile
      [INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.0.3.RELEASE:compile
      [INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.0.3.RELEASE:compile
      [INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.3.RELEASE:compile
      [INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.3.RELEASE:compile
      [INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
      [INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
      [INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.10.0:compile
      [INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
      [INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
      [INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
      [INFO] |  |  \- org.yaml:snakeyaml:jar:1.19:runtime
      [INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:2.0.3.RELEASE:compile
      [INFO] |  |  +- org.springframework:spring-aop:jar:5.0.7.RELEASE:compile
      [INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.13:compile
      [INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.0.3.RELEASE:compile
      [INFO] |  |  +- com.zaxxer:HikariCP:jar:2.7.9:compile
      [INFO] |  |  \- org.springframework:spring-jdbc:jar:5.0.7.RELEASE:compile
      [INFO] |  +- org.hibernate:hibernate-core:jar:5.2.17.Final:compile
      [INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
      [INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.2.Final:compile
      [INFO] |  |  +- org.javassist:javassist:jar:3.22.0-GA:compile
      [INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
      [INFO] |  |  +- org.jboss:jandex:jar:2.0.3.Final:compile
      [INFO] |  |  +- com.fasterxml:classmate:jar:1.3.4:compile
      [INFO] |  |  +- dom4j:dom4j:jar:1.6.1:compile
      [INFO] |  |  \- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.Final:compile
      [INFO] |  +- javax.transaction:javax.transaction-api:jar:1.2:compile
      [INFO] |  +- org.springframework.data:spring-data-jpa:jar:2.0.8.RELEASE:compile
      [INFO] |  |  +- org.springframework.data:spring-data-commons:jar:2.0.8.RELEASE:compile
      [INFO] |  |  +- org.springframework:spring-orm:jar:5.0.7.RELEASE:compile
      [INFO] |  |  +- org.springframework:spring-context:jar:5.0.7.RELEASE:compile
      [INFO] |  |  |  \- org.springframework:spring-expression:jar:5.0.7.RELEASE:compile
      [INFO] |  |  +- org.springframework:spring-tx:jar:5.0.7.RELEASE:compile
      [INFO] |  |  \- org.springframework:spring-beans:jar:5.0.7.RELEASE:compile
      [INFO] |  \- org.springframework:spring-aspects:jar:5.0.7.RELEASE:compile
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-13
        • 2017-08-03
        • 2020-11-02
        相关资源
        最近更新 更多