# DB2 Named Queries different upon Hibernate 6 upgrade

**URL:** <https://discourse.hibernate.org/t/db2-named-queries-different-upon-hibernate-6-upgrade/11794>\
**Category:** Hibernate ORM\
**Created:** [September 10, 2025, 5:00pm UTC](https://discourse.hibernate.org/t/db2-named-queries-different-upon-hibernate-6-upgrade/11794 "2025-09-10T17:00:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bfett1999](https://avatars.discourse-cdn.com/v4/letter/b/5fc32e/32.png) [@bfett1999](https://discourse.hibernate.org/u/bfett1999)\
**Post date:** [September 10, 2025, 5:00pm UTC](https://discourse.hibernate.org/t/db2-named-queries-different-upon-hibernate-6-upgrade/11794/1 "2025-09-10T17:00:54Z")

</div>

After upgrading from hibernate 5 to hibernate 6 (6.6.28.Final), DB2 Named queries that had the following sql are drastically changed into a completely different query.

The named query looks something like this:

```auto
select A from <entity> A
where (A.id, A.dateTime) in (
    select B.id, max(B.dateTime) from <entity> B
    group by B.id
)

```

In hibernate 5, it would be translated to the following SQL:

```auto
select <ID_COLUMN>, <DATE_TIME_COLUMN>, …, <OTHER COLUMN n>
from <table> A
where (A.<ID_COLUMN>, A.<DATE_TIME_COLUMN>) in (
    select B.<ID_COLUMN>, max(B.<DATE_TIME_COLUMN>) from <table> B
    group by B.<ID_COLUMN>
)

```

In hibernate 6, it is getting translated to a completely different SQL:

```auto
select <ID_COLUMN>, <DATE_TIME_COLUMN>, …, <OTHER COLUMN n>
from <table> A
exists (
    select B.<ID_COLUMN>, max(B.<DATE_TIME_COLUMN>) from <table> B
    group by B.<ID_COLUMN>
)
having B.<ID_COLUMN> = A.<ID_COLUMN>
and max(B.DATE_TIME_COLUMN) = A.DATE_TIME_COLUMN

```

I have confirmed that I am still using DB2Dialect in hibernate 6 that I was using in hibernate5. One odd thing I did notice is that the hibernate ORM log says 7.0.3, where the hibernate version is 6.6.28. Is this expected?

Is this a known issue? Is there anything I can do to get the query to generate the same way as it did in hibernate 5?

Thanks!

---

<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:** [September 11, 2025, 10:01am UTC](https://discourse.hibernate.org/t/db2-named-queries-different-upon-hibernate-6-upgrade/11794/2 "2025-09-11T10:01:57Z")

</div>

Hi and thanks for reporting the problem. I looked into it and figured, that something got lost in the development of ORM 6.0. I created this Jira issue that you can track: [HHH-19768](https://hibernate.atlassian.net/browse/HHH-19768)
