# Malformed Query for DB2 i with 6.2.1 release

**URL:** <https://discourse.hibernate.org/t/malformed-query-for-db2-i-with-6-2-1-release/7157>\
**Category:** Hibernate ORM\
**Created:** [January 17, 2023, 2:43pm UTC](https://discourse.hibernate.org/t/malformed-query-for-db2-i-with-6-2-1-release/7157 "2023-01-17T14:43:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveIreland](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SteveIreland](https://discourse.hibernate.org/u/SteveIreland)\
**Post date:** [January 17, 2023, 2:43pm UTC](https://discourse.hibernate.org/t/malformed-query-for-db2-i-with-6-2-1-release/7157/1 "2023-01-17T14:43:48Z")

</div>

I connect to both MySQL and an IBM i and recently upgraded to org.hibernate.orm:hibernate-core:6.1.5.Final. Queries that were working fine in the earlier release started generating malformed SQL statements resulting in a plethora of …

_“Caused by: com.ibm.as400.access.AS400JDBCSQLSyntaxErrorException: [SQL0199] Keyword BY not expected. Valid tokens: FOR USE SKIP WAIT WITH FETCH LIMIT ORDER UNION EXCEPT OFFSET.”_

… errors.

Since I’m on a deadline I’m going to revert all of these statements back to createNativeQuery() statements from createQuery() statements, but I was wondering if anyone knows about this issue and can share a quick solution with me.

I did switch to the DB2iDialect.

Thanks.

---

<div class="post-metadata">

**Author:** ![SteveIreland](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@SteveIreland](https://discourse.hibernate.org/u/SteveIreland)\
**Post date:** [January 18, 2023, 5:43pm UTC](https://discourse.hibernate.org/t/malformed-query-for-db2-i-with-6-2-1-release/7157/2 "2023-01-18T17:43:06Z")

</div>

Just to expand on the issue a bit. The following simple JPA statement …

```auto
            String s = "select ho from HostOrder ho where ho.orderNumber = :orderNumber ";
            Query q = em.createQuery(s, HostOrder.class);
            q.setParameter("orderNumber", orderNumber);

```

results in the following generated SQL …

```auto
select * from (select h1_0.CEOPNB c0,h1_0.CEACC3 c1,h1_0.CEFUQT c2,h1_0.CETGQT c3,
h1_0.CEBUS c4,h1_0.CECHD c5,h1_0.CECHT c6,h1_0.CECHU c7,h1_0.CECRD c8,h1_0.CECRT c9,
h1_0.CECRU c10,h1_0.CEMWNB c11,h1_0.CERSNB c12,h1_0.CENVDT c13,h1_0.CERXCD c14,h1_0.CEH1CD c15,
h1_0.CEOQNB c16,h1_0.CEKSCD c17,h1_0.CEW1PR c18,h1_0.CENSCD c19,h1_0.CENANB c20,
h1_0.CEA0VN c21,h1_0.CEDMCD c22,h1_0.CEA1VN c23,h1_0.CEIAST c24,h1_0.CEZMNB c25,
h1_0.CEZENB c26,h1_0.CEIBST c27,h1_0.CEKRCD c28,h1_0.CEGYST c29,h1_0.CEQHDT c30,
h1_0.CEZYNB c31,h1_0.CEAIC1 c32,h1_0.CEIDCD c33,h1_0.CEDYVA c34,h1_0.CEAST c35,
h1_0.CEFSST c36,h1_0.CETSNB c37,h1_0.CEZDNB c38,h1_0.CEPSPR c39,h1_0.CEORNB c40,
h1_0.CEORHN c41,h1_0.CEOBDT c42,h1_0.CEAETM c43,
row_number() over() rn 
from MD0026AA0D.ORHP h1_0 where h1_0.CEOPNB=?) r_0_ where order by r_0_.rn

```

Of course, the “where order by” isn’t valid SQL on any platform I’m aware of, but it certainly isn’t on the DB2 platform.

---

<div class="post-metadata">

**Author:** ![mbladel](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/mbladel/32/2128_2.png) [@mbladel](https://discourse.hibernate.org/u/mbladel)\
**Post date:** [January 20, 2023, 2:52pm UTC](https://discourse.hibernate.org/t/malformed-query-for-db2-i-with-6-2-1-release/7157/3 "2023-01-20T14:52:09Z")

</div>

Hello, your issue should have been addressed by [[HHH-15890] - Hibernate JIRA](https://hibernate.atlassian.net/browse/HHH-15890) and will be fixed in the next versions of Hibernate (6.2.0 and 6.1.7).

Please let us know if the problem persists after the release.
