为什么休眠模式无法与Logback一起使用?

时间:2019-03-29 06:02:54

标签: java spring hibernate logback

我正在使用以下登录配置。 我的项目基于jpa 2.0.13的春季数据。

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </encoder>
    </appender>
    <logger name="org.hibernate.SQL" level="debug" additivity="false">
        <appender-ref ref="STDOUT"/>
    </logger>
    <logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">
        <appender-ref ref="STDOUT"/>
    </logger>
    <root level="debug">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

我在控制台中看不到任何 CRUD sql日志,例如select语句。将 hibernate.show_sql 设置为true时,它可以工作,但是由于我更喜欢​​使用logback,因此我将其禁用。 我可以从spring和我的业务代码中看到日志,为什么休眠不起作用?

下面是我的行家树。

[INFO] 
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ storybook ---
[INFO] com.demo.test:storybook:war:2.5.0
[INFO] +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] +- ch.qos.logback:logback-classic:jar:1.2.3:compile
[INFO] |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
[INFO] +- log4j:apache-log4j-extras:jar:1.2.17:compile
[INFO] |  \- log4j:log4j:jar:1.2.17:compile
[INFO] +- javax.jms:jms:jar:1.1:compile
[INFO] +- xerces:xercesImpl:jar:2.10.0:compile
[INFO] |  \- xml-apis:xml-apis:jar:1.4.01:compile
[INFO] +- io.springfox:springfox-swagger2:jar:2.5.0:compile
[INFO] |  +- io.swagger:swagger-annotations:jar:1.5.9:compile
[INFO] |  +- io.swagger:swagger-models:jar:1.5.9:compile
[INFO] |  +- io.springfox:springfox-spi:jar:2.5.0:compile
[INFO] |  |  \- io.springfox:springfox-core:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-schema:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-swagger-common:jar:2.5.0:compile
[INFO] |  +- io.springfox:springfox-spring-web:jar:2.5.0:compile
[INFO] |  +- com.google.guava:guava:jar:20.0:compile
[INFO] |  +- com.fasterxml:classmate:jar:1.3.4:compile
[INFO] |  +- org.springframework.plugin:spring-plugin-core:jar:1.2.0.RELEASE:compile
[INFO] |  +- org.springframework.plugin:spring-plugin-metadata:jar:1.2.0.RELEASE:compile
[INFO] |  \- org.mapstruct:mapstruct:jar:1.0.0.Final:compile
[INFO] +- org.zenframework.z8.dependencies.commons:ojdbc6:jar:2.0:compile
[INFO] +- io.springfox:springfox-swagger-ui:jar:2.5.0:compile
[INFO] +- javax.mail:mail:jar:1.4:compile
[INFO] +- concurrent:concurrent:jar:1.3.4:compile
[INFO] +- com.jcraft:jsch:jar:0.1.54:compile
[INFO] +- commons-digester:commons-digester:jar:2.1:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.2:compile
[INFO] +- org.apache.xmlbeans:xmlbeans:jar:2.6.0:compile
[INFO] |  \- stax:stax-api:jar:1.0.1:compile
[INFO] +- commons-fileupload:commons-fileupload:jar:1.3.3:compile
[INFO] +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] +- commons-beanutils:commons-beanutils:jar:1.9.3:compile
[INFO] |  \- commons-collections:commons-collections:jar:3.2.2:compile
[INFO] +- commons-codec:commons-codec:jar:1.11:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.8:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.8:compile
[INFO] +- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.13:compile
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- javax.activation:activation:jar:1.1.1:compile
[INFO] +- org.springframework:spring-webmvc:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-beans:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:5.0.12.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-jcl:jar:5.0.12.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:5.0.12.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:5.0.12.RELEASE:compile
[INFO] +- com.h2database:h2:jar:1.4.197:compile
[INFO] +- org.hibernate:hibernate-entitymanager:jar:5.2.17.Final:compile
[INFO] |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:5.2.17.Final:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  \- org.jboss:jandex:jar:2.0.3.Final:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  +- org.hibernate.common:hibernate-commons-annotations:jar:5.0.1.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] |  +- net.bytebuddy:byte-buddy:jar:1.7.11:compile
[INFO] |  \- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.1.Final:compile
[INFO] \- org.springframework.data:spring-data-jpa:jar:2.0.13.RELEASE:compile
[INFO]    +- org.springframework.data:spring-data-commons:jar:2.0.13.RELEASE:compile
[INFO]    +- org.springframework:spring-orm:jar:5.0.12.RELEASE:compile
[INFO]    |  \- org.springframework:spring-jdbc:jar:5.0.12.RELEASE:compile
[INFO]    +- org.springframework:spring-tx:jar:5.0.12.RELEASE:compile
[INFO]    \- org.aspectj:aspectjrt:jar:1.8.13:compile

下面是我的spring dbconfig。

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
  entityManagerFactoryRef = "h2_entityManagerFactory",
  basePackages = { "com.demo.test.repository" },
        transactionManagerRef = "h2_manager"

)
public class H2DBConfig {

  @Primary
  @Bean(name = "h2_dataSource")
  public DataSource dataSource(){
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName("org.h2.Driver");

      dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
      dataSource.setUsername("");
      dataSource.setPassword("");
      return dataSource;
  }

  @Primary
  @Bean(name = "h2_entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean h2EntityManagerFactory() {
      LocalContainerEntityManagerFactoryBean em
              = new LocalContainerEntityManagerFactoryBean();
      em.setDataSource(dataSource());
      em.setPackagesToScan(new String[] { "com.demo.test.entity" });
      em.setPersistenceUnitName("h2_unit");
      JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      em.setJpaVendorAdapter(vendorAdapter);
      em.setJpaProperties(additionalProperties());

      return em;
  }

  Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
//        properties.setProperty("hibernate.show_sql", "true");
        return properties;
    }


    @Bean(name = "h2_manager")
    public PlatformTransactionManager transactionManager(
            @Qualifier("h2_entityManagerFactory") EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        return transactionManager;
    }
}

0 个答案:

没有答案