Hibernate 查询日志记录 - 从实体名称记录

时间:2021-06-16 11:19:03

标签: java spring hibernate spring-data-jpa spring-data

我通过在 application.properties 中设置以下配置在 Hibernate 中使用查询日志

logging.level.org.springframework.transaction.interceptor=DEBUG
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=DEBUG

这很好用并记录查询。我需要通过在我从哪个表中选择数据的开头添加一个表名来增强此日志记录。例如。我记录了以下查询:

2021-06-16 13:11:18.008 DEBUG 6544 --- [io-8080-exec-10] org.hibernate.SQL : select count(program0_.id) as col_0_0_ from public.program program0_ where 1=1
2021-06-16 13:11:18.008 DEBUG 6544 --- [io-8080-exec-10] org.hibernate.SQL : select count(user_0 .id) as col_0_0_ from public.user user_0 where 1=1

这些查询很简单,很容易确定从表 programuser 查询数据。但在大查询的情况下,以表名开头可以简化日志检查。

是否有任何选项可以在 Hibernate 中进行设置?

1 个答案:

答案 0 :(得分:0)

  1. Hibernate 没有这样的选项。

  2. 评论已经描述了需求的定义相当不明确,因为您可以在任何 SQL 语句中拥有任意数量的表,包括 0,因此通用解决方案相当棘手。

  3. 但是你可以自己做这样的事情。

    例如,如果您使用 Logback,您可能会注册一个自定义 layout 来解析消息,如果它找到一条 SQL 语句,它就会从中提取表。

    或者,您可以使用用于收集和整合日志的任何工具链对日志进行后期处理