当前位置: 首页 > 知识库问答 >
问题:

如何在Hibernate[重复]中打印带值的select查询

壤驷承
2023-03-14
<spring.version>5.2.0.RELEASE</spring.version>
<spring-boot.version>2.2.0.RELEASE</spring-boot.version>
<spring-boot-dev-tools.version>2.2.6.RELEASE</spring-boot-dev-tools.version>
<hibernate.version>5.4.6.Final</hibernate.version>
<spring-data.version>2.2.0.RELEASE</spring-data.version>
#Hibernate Properties
hibernate.show_sql=true
hibernate.use_sql_comments=true
hibernate.format_sql=true
hibernate.type=trace
hibernate.dialect=org.hibernate.dialect.SybaseASE15Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

我的log4j.xml有以下内容:

    <logger name="org.hibernate">
        <level value="TRACE"/>
    </logger>
    <logger name="org.hibernate.sql">
        <level value="TRACE"/>
    </logger>
    <logger name="org.hibernate.type">
        <level value="TRACE"/>
    </logger>

即使在设置了这些配置之后,我仍然没有看到包含值的精确的select查询。我还有一种感觉,日志记录级别实际上不起作用,因为我没有看到任何来自Hibernate的过度日志记录。此外,我多次构建maven只是为了确保构建了新属性。这是日志的外观:

INFO  2020-06-08 11:35:41.392 [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate  - Bootstrapping Spring Data repositories in DEFAULT mode.
INFO  2020-06-08 11:35:41.477 [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate  - Finished Spring Data repository scanning in 60ms. Found 0 repository interfaces.
INFO  2020-06-08 11:35:41.482 [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate  - Bootstrapping Spring Data repositories in DEFAULT mode.
INFO  2020-06-08 11:35:41.655 [main] org.springframework.data.repository.config.RepositoryConfigurationDelegate  - Finished Spring Data repository scanning in 172ms. Found 2 repository interfaces.
INFO  2020-06-08 11:35:45.855 [MLog-Init-Reporter] com.mchange.v2.log.MLog  - MLog clients using slf4j logging.
INFO  2020-06-08 11:35:46.099 [main] com.mchange.v2.c3p0.C3P0Registry  - Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
INFO  2020-06-08 11:35:48.331 [main] org.hibernate.Version  - HHH000412: Hibernate Core {5.4.6.Final}
INFO  2020-06-08 11:35:48.622 [main] org.hibernate.annotations.common.Version  - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
INFO  2020-06-08 11:35:53.250 [main] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean  - Initialized JPA EntityManagerFactory for persistence unit 'MyServer'
INFO  2020-06-08 11:35:54.075 [main] com.datastax.driver.core  - DataStax Java driver 3.7.1 for Apache Cassandra
INFO  2020-06-08 11:35:54.119 [main] com.datastax.driver.core.GuavaCompatibility  - Detected Guava >= 19 in the classpath, using modern compatibility layer
INFO  2020-06-08 11:35:55.011 [main] com.datastax.driver.core.Native  - Could not load JNR C Library, native system calls through this library will not be available (set this logger level to DEBUG to see the full stack trace).
INFO  2020-06-08 11:35:55.012 [main] com.datastax.driver.core.ClockFactory  - Using java.lang.System clock to generate timestamps.
INFO  2020-06-08 11:35:55.847 [main] org.quartz.impl.StdSchedulerFactory  - Using default implementation for ThreadExecutor
INFO  2020-06-08 11:35:55.936 [main] org.quartz.core.SchedulerSignalerImpl  - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
INFO  2020-06-08 11:35:55.940 [main] org.quartz.core.QuartzScheduler  - Quartz Scheduler v.2.1.7 created.
INFO  2020-06-08 11:35:55.944 [main] org.quartz.simpl.RAMJobStore  - RAMJobStore initialized.
INFO  2020-06-08 11:35:55.948 [main] org.quartz.core.QuartzScheduler  - Scheduler meta-data: Quartz Scheduler (v2.1.7) 'quartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

INFO  2020-06-08 11:35:55.948 [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler 'quartzScheduler' initialized from an externally provided properties instance.
INFO  2020-06-08 11:35:55.948 [main] org.quartz.impl.StdSchedulerFactory  - Quartz scheduler version: 2.1.7
INFO  2020-06-08 11:35:55.948 [main] org.quartz.core.QuartzScheduler  - JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@136690b1
INFO  2020-06-08 11:35:56.029 [main] org.springframework.scheduling.quartz.SchedulerFactoryBean  - Starting Quartz Scheduler now
INFO  2020-06-08 11:35:56.029 [main] org.quartz.core.QuartzScheduler  - Scheduler quartzScheduler_$_NON_CLUSTERED started.
INFO  2020-06-08 11:35:56.038 [main] com.abcde.MyServer  - Started MyServer in 16.772 seconds (JVM running for 20.814)

INFO  2020-06-08 11:35:59.927 [Log4J Redirect INFO-1] com.xcxcx.util.Log4JAppender$SystemOutProxy  - Hibernate: 
    /* dynamic native SQL query */ select
        rp.*
    from
        abcde.dbo.table1 pe,
        abcde.dbo.table2 rp 
    where
        pe.a = ? 
        and pe.b = ? 
        and rp.a = pe.a 
        and rp.b = pe.b 
        and rp.c = pe.c 

INFO  2020-06-08 11:36:00.098 [Log4J Redirect INFO-1] com.abcde.util.Log4JAppender$SystemOutProxy  - Hibernate: 
    select
        // columns defined for select
    from
        abcde.dbo.table2 table2i0_ 
    where
        table2i0_.a=? 
        and table2i0_.b=?
WARN  2020-06-08 11:36:00.277 [main] com.abcde.db.dbconnect.DBConnectUtil  - Exception during statement execution 
com.sybase.jdbc4.jdbc.SybSQLException: Implicit conversion from datatype 'VARCHAR' to 'INT' is not allowed.  Use the CONVERT function to run this query.
at com.sybase.jdbc4.tds.Tds.processEed(Tds.java:4119)
    at com.sybase.jdbc4.tds.Tds.nextResult(Tds.java:3235)
    at com.sybase.jdbc4.jdbc.ResultGetter.nextResult(ResultGetter.java:78)
    at com.sybase.jdbc4.jdbc.SybStatement.nextResult(SybStatement.java:294)
    at com.sybase.jdbc4.jdbc.SybStatement.nextResult(SybStatement.java:276)
    at com.sybase.jdbc4.jdbc.SybStatement.queryLoop(SybStatement.java:2643)
    at com.sybase.jdbc4.jdbc.SybStatement.executeQuery(SybStatement.java:2629)
    at com.sybase.jdbc4.jdbc.SybPreparedStatement.executeQuery(SybPreparedStatement.java:274)

共有1个答案

林项明
2023-03-14

在appication.properties中的src/main/resources下,将此

logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

logging.level.org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl=TRACE

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="SQL_CONSOLE" class="ch.qos.logback.core.ConsoleAppender" >
    <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
      <evaluator>        
        <matcher>
          <Name>allow</Name>
          <regex>.*Closing\sprepared\sstatement.*</regex>
        </matcher>
        <expression>allow.matches(formattedMessage)</expression>
      </evaluator>
      <OnMismatch>DENY</OnMismatch>
      <OnMatch>ACCEPT</OnMatch>
    </filter>
    <encoder>
        <pattern>---SQL[START]----\n%d{"yyyy-MM-dd'T'HH:mm:ss,SSS"} %replace(%msg){'Closing prepared statement',''}%n-----SQL[END]----\n</pattern>
        <outputPatternAsHeader>false</outputPatternAsHeader>
      </encoder>     
  </appender>
  <appender name="DEFAULT_CONSOLE" class="ch.qos.logback.core.ConsoleAppender" >
  <encoder>
        <pattern>%logger[0]: %msg%n</pattern>
        <outputPatternAsHeader>false</outputPatternAsHeader>
      </encoder>   
  </appender>
  <logger level="TRACE" name="org.hibernate.resource.jdbc.internal.ResourceRegistryStandardImpl" additivity="false">
    <appender-ref ref="SQL_CONSOLE" />
  </logger>
  <root level="ERROR">
    <appender-ref ref="DEFAULT_CONSOLE" />
  </root>
</configuration>
 类似资料:
  • 我有一个哈希图,其中一些键指向相同的值。我想找到所有相等的值并打印相应的键。 这是我拥有的当前代码: 当前代码将重复项两次添加到列表中,但它也将每个键添加一次。 谢谢。

  • 问题内容: 我正在尝试修复这段代码,该代码是从具有车牌号和车主列表(该格式)的哈希图进行打印的。我试图通过printOwners()仅打印出所有者;但是我不能不打印重复的东西。 我已经玩了一段时间,但似乎无法跳过重复项。 这是我的代码: 问题答案: 要删除重复项,请使用: 或者使用Java 8 和方法:

  • 我希望在下面的代码中输出一个文本而不是: ie我希望输出为

  • 问题内容: 在Hibernate中是否可以用实际值而不是问号打印生成的SQL查询? 如果Hibernate API无法实现,您将如何建议使用实值打印查询? 问题答案: 您需要为以下类别启用日志记录: -设置为在执行所有SQL DML语句时记录它们 -设置为记录所有JDBC参数 因此,log4j配置如下所示: 第一个等效于legacy 属性,第二个则 打印绑定参数。 另一个解决方案(基于非hiber

  • 问题内容: 我有一些键指向相同值的哈希图。我想找到所有相等的值并打印相应的键。 这是我目前的代码: 当前代码将重复项两次添加到列表中,但是也会将每个键一次添加一次。 谢谢。 问题答案: 您可以使用流以这种方式检索重复项: 然后,您可以使用以下命令将其打印出来:

  • 问题内容: 在Hibernate中是否可以用实际值而不是问号打印生成的SQL查询? 如果Hibernate API无法实现,您将如何建议使用实值打印查询? 问题答案: 您需要为以下类别启用日志记录: -设置为在执行所有SQL DML语句时记录它们 -设置为记录所有JDBC参数 因此,log4j配置如下所示: 第一个等效于 属性,第二个则打印绑定参数。 另一个解决方案(基于非hibernate)是使