【发布时间】:2021-09-01 05:07:44
【问题描述】:
我通过在我的 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
这些查询很简单,很容易确定是从表program 和user 中查询到的数据。但是在大查询的情况下,开头的表名会简化日志的检查。
有什么选项可以在 Hibernate 中设置吗?
【问题讨论】:
-
如果选择了多个表中的数据会怎样呢?或者甚至没有使用表,但使用了 CTE,或者......起初,您的解决方案可能听起来是个好主意,对于复杂的用例,它会开始快速崩溃/恶化。
-
你总是在'from'子句中指定一个表,所以我想看看那里指定的表。
-
如果没有使用表,该字段保持为空。
-
好吧,在 CTE 的情况下,它只是该查询的别名。但是,如果不解析字符串并希望有一个 FROM 你可以相信它是行不通的。
标签: java spring hibernate spring-data-jpa spring-data