如何在PagingAndSortingRepository中记录@Query?

时间:2016-09-16 15:32:47

标签: spring hibernate logging spring-boot spring-data

有没有办法记录一些自定义 @Query方法

以下是我的代码示例:

@Query(value = "SELECT * FROM transfer WHERE char_length(internal_id) = 5 " +
        "AND internal_id REGEXP '^[0-9]+$' AND project_id = :projectId order by created_at desc limit 1", nativeQuery = true)
Transfer findLastWithDefaultOurIdForProject(@Param("projectId") String projectId);

它在 interface 中编写,扩展了 spring-data PagingAndSortingRepository

我试图通过在属性文件中添加这些行来记录它: log4j.logger.org.hibernate.SQL=DEBUG log4j.logger.org.hibernate.type=TRACE

但我只收到没有从我的服务传递到存储库界面的实际值的查询?

2 个答案:

答案 0 :(得分:3)

尝试此配置:

application.properties

spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database=h2

logback.xml下添加src/main/resources文件以配置 Hibernate 以显示传递给SQL查询的参数:

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <logger name="org.hibernate.SQL" additivity="false" >
        <level value="DEBUG" />
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="org.hibernate.type" additivity="false" >
        <level value="TRACE" />
        <appender-ref ref="STDOUT" />
    </logger>

</configuration>

您可以在我的GitHub存储库中找到工作Demo Project

答案 1 :(得分:0)

问题是您的应用程序属性。 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-levels

属性上有错误的前缀。

logging.level.org.hibernate.SQL=DEBUG logging.level.org.hibernate.type=TRACE