# Loss of getQueryString() method moving from v5 to v6

**URL:** <https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933>\
**Category:** Miscellaneous\
**Created:** [January 30, 2024, 6:37pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933 "2024-01-30T18:37:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcookcrx](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dcookcrx](https://discourse.hibernate.org/u/dcookcrx)\
**Post date:** [January 30, 2024, 6:37pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/1 "2024-01-30T18:37:59Z")

</div>

Moving from Spring Boot 2 to Spring Boot 3 forces a move from Hibernate 5 to Hibernate 6.

We have an application that uses a dynamically constructed CriteriaQuery to display data in a UI, but then to allow download of the data we make use of PostgreSQL UNLOAD command to send the data to an AWS S3 bucket. The generation of the UNLOAD commands requires the translation of the CriteraQuery to String representing the corresponding SQL SELECT statement.

The translation is currently being done with the getQueryMethod() after unwrapping the TypedQuery. A direct replacement seems to be missing, does anyone know how this might be accomplished within Hibernate 6?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [January 31, 2024, 6:16pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/2 "2024-01-31T18:16:18Z")

</div>

This is not so easy. A `CriteriaQuery` or HQL query could be split into multiple separate queries (polymorphic query). The final SQL might depend on parameter values when e.g. parameters are used in a context that doesn’t allow untyped expressions and the parameter type is unknown.  
So if you want to get your hands on the SQL with ORM 6, you will have to do what ORM 6 does internally and translate the query. Take a look into `org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan` which is the entry point for that whole translation process. You need to get your hands onto a `JdbcOperationQuerySelect` which has a `getSqlString()` method.

---

<div class="post-metadata">

**Author:** ![bmaehr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/bmaehr/32/3516_2.png) [@bmaehr](https://discourse.hibernate.org/u/bmaehr)\
**Post date:** [October 11, 2024, 4:23pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/3 "2024-10-11T16:23:05Z")

</div>

I also used the getQueryString() and miss it now.  
It was used just for writing tests and validating that the Query build by Criteria Api is correct but still very useful.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 22, 2024, 2:46pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/4 "2024-10-22T14:46:15Z")

</div>

You can use `org.hibernate.resource.jdbc.spi.StatementInspector` to get a callback when statements are executed. Store that into a `ThreadLocal<List<String>>` and then you can check what SQL queries were executed.

---

<div class="post-metadata">

**Author:** ![dcookcrx](https://avatars.discourse-cdn.com/v4/letter/d/ec9cab/32.png) [@dcookcrx](https://discourse.hibernate.org/u/dcookcrx)\
**Post date:** [October 22, 2024, 5:02pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/5 "2024-10-22T17:02:24Z")

</div>

The `org.hibernate.resource.jdbc.spi.StatementInspector` would not work in my case. The application handles an incoming request by creating the CriteriaQuery but then branches based upon the expected output. For a normal result the query is executed and the result translated to JSON for the response. There is another output option where the output is sent to an AWS S3 bucket. This is done by wrapping the text of the SQL query statement inside and UNLOAD function along with the target parameters. Do this feature in the application we cannot migrate to Hibernate v6 without an entire rewrite of the application.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 22, 2024, 5:14pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/6 "2024-10-22T17:14:54Z")

</div>

Please create a new topic and share some more details about this use case. I have no idea what this `unload` call looks like or what you need, but maybe there is a way to get this working.

---

<div class="post-metadata">

**Author:** ![bmaehr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/bmaehr/32/3516_2.png) [@bmaehr](https://discourse.hibernate.org/u/bmaehr)\
**Post date:** [November 1, 2024, 9:34pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/8 "2024-11-01T21:34:18Z")

</div>

Should the `org.hibernate.resource.jdbc.spi.StatementInspector` be called for statments from the critria api? Because in my case it I was not able to see that - it was only called for normal statements.  
About your other suggestion: If I would find the needed information somewhere in `org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan`, how would I get the `org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan` object?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 5, 2024, 7:16pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/9 "2024-11-05T19:16:57Z")

</div>

Yes, `StatementInspector` is called for all statements. You can construct a `ConcreteSqmSelectQueryPlan` similar to how `org.hibernate.query.sqm.internal.QuerySqmImpl#resolveSelectQueryPlan` does it.

---

<div class="post-metadata">

**Author:** ![bmaehr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/bmaehr/32/3516_2.png) [@bmaehr](https://discourse.hibernate.org/u/bmaehr)\
**Post date:** [February 8, 2025, 9:39pm UTC](https://discourse.hibernate.org/t/loss-of-getquerystring-method-moving-from-v5-to-v6/8933/10 "2025-02-08T21:39:29Z")

</div>

The solution is in fact really simple - you just need to find it:

```auto
        final Query unwrappedQuery = typedQuery.unwrap(Query.class);
        final SqmSelectStatement sqmStatement = (SqmSelectStatement) ((SqmQuery) unwrappedQuery).getSqmStatement();
        String result = sqmStatement.toHqlString();

```
