Spring Boot Data JPA是否在内部使用休眠模式来实现?

时间:2018-12-05 07:49:50

标签: spring hibernate jpa spring-data-jpa

我正在努力建立一个现有项目,我在pom.xml中发现了以下依赖项

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

但是休眠没有任何依赖关系,因此如何检查项目是否使用休眠。

并且我还发现了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引导数据JPA是否带有默认的Hibernate配置?。

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

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果您使用的是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

如果您在项目上执行mvndependency:tree来查看spring jpa引入的依赖项,您将看到休眠状态:

 +- 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