Can I make Hibernate label the SQL it produces with query names?

When analyzing slow query reports from the database it is a bit tricky to associate the SQL to Java code that issues them.

Is it possible to configure Hibernate to attach a label (as a comment) to the SQL it issues? Ideally, for JPA NamedQueries it would be the query name, for dynamically constructed queries some label that we can tell the CriteriaBuilder to use.


You can attach comment to the query through Criteria for analyzing queries, if I understood it correctly.

setComment(String comment) : Add a comment to the generated SQL.

Parameters:comment - a human-readable string

Returns:this (for method chaining)

For other cases, not using Criteria API, can add comment as hint to the query.

setHint(String hintName, Object value) : Set a query property or hint. The hints elements may be used to specify query properties and hints. Properties defined by this specification must be observed by the provider. Vendor-specific hints that are not recognized by a provider must be silently ignored . Portable applications should not rely on the standard timeout hint. Depending on the database in use and the locking mechanisms used by the provider, this hint may or may not be observed .

[Emphasis mine]

Depending on the implementation can add hint to the query, can refer here for Hibernate. Also, there is property available org.hibernate.comment can be used to set comment into hint.

链接地址: http://www.djcxy.com/p/63824.html

上一篇: Hibernate的位置参数基于零

下一篇: 我可以让Hibernate标签它与查询名称产生的SQL吗?