Why does Hibernate throw a QuerySyntaxException for this HQL?

Default

While building a query using Hibernate, I noticed something rather odd. If I use sequential named parameters for the ORDER BY clause, Hibernate throws a QuerySyntaxException (the colon prefix being an unexpected token):

createQuery("FROM MyEntity ORDER BY :orderProperty :orderDirection");

However, when this is done with a plain SQL query the query is created without a problem:

createSQLQuery("SELECT * FROM my_entity_table ORDER BY :orderProperty :orderDirection");

I know Hibernate is doing more String evaluation for the HQL query, which is probably why the SQL query is created without an error. I am just wondering why Hibernate would care that there are two sequential named parameters.

This isn't a huge issue since it is simple to work around (can just append the asc or desc String value to the HQL instead of using a named paramater for it), but it struck my curiosity why Hibernate is preventing it (perhaps simply because 99% of the time sequential named parameters like this result in invalid SQL/HQL).

Mason T.

I've been testing this in my local, and I can't get your desired outcome to work with HQL.

Here is quote from the post I linked:

You can't bind a column name as a parameter. Only a column value. This name has to be known when the execution plan is computed, before binding parameter values and executing the query. If you really want to have such a dynamic query, use the Criteria API, or some other way of dynamically creating a query.

Criteria API looks to be the more useful tool for your purposes.

Here is an example:

    Criteria criteria = session.createCriteria(MyEntity.class);

    if (orderDirection.equals("desc")) {
        criteria.addOrder(Order.desc(orderProperty));
    }
    else {
        criteria.addOrder(Order.asc(orderProperty));
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Hibernate - org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped

From Dev

org.hibernate.hql.ast.QuerySyntaxException: unexpected token:

From Java

org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped

From Dev

How to solve org.hibernate.hql.ast.QuerySyntaxException: Exception

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped

From Dev

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token

From Dev

Why does Hibernate throw org.hibernate.exception.LockAcquisitionException?

From Dev

Selecting nested Hibernate immutable objects throws org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: new

From Dev

I got an org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped exception

From Dev

Spring, Spring Data JPA: org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: '1.id'

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: EdbmsEmployee is not mapped [from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?]

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: Apr near line 1, column 81

From Dev

Spring boot org.hibernate.hql.internal.ast.QuerySyntaxException: Utilisateur is not mapped (with MYSQL)

From Dev

Spring boot multiple module project org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped

From Dev

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: '1.id'

From Dev

Spring boot org.hibernate.hql.internal.ast.QuerySyntaxException: Utilisateur is not mapped (with MYSQL)

From Dev

I got an org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped exception

From Dev

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException MS-Sql Server

From Dev

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 17

From Dev

Hibernate-Mysql QuerySyntaxException

From Dev

Hibernate QuerySyntaxException, Table not mapped

From Dev

org.hibernate.hql.ast.QuerySyntaxException:on near line 1, column 102 [FROM com.strutsProject.dto.EventClubTb

From Dev

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 17 for absolutely unclear reasons

From Dev

org.hibernate.hql.ast.QuerySyntaxException:on near line 1, column 102 [FROM com.strutsProject.dto.EventClubTb

From Dev

Why does this code not throw a NullPointerException?

Related Related

  1. 1

    Hibernate - org.hibernate.hql.internal.ast.QuerySyntaxException: Client is not mapped

  2. 2

    org.hibernate.hql.ast.QuerySyntaxException: unexpected token:

  3. 3

    org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped

  4. 4

    How to solve org.hibernate.hql.ast.QuerySyntaxException: Exception

  5. 5

    org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped

  6. 6

    org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor

  7. 7

    org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token

  8. 8

    Why does Hibernate throw org.hibernate.exception.LockAcquisitionException?

  9. 9

    Selecting nested Hibernate immutable objects throws org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: new

  10. 10

    I got an org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped exception

  11. 11

    Spring, Spring Data JPA: org.hibernate.hql.internal.ast.QuerySyntaxException: Test is not mapped

  12. 12

    org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: '1.id'

  13. 13

    org.hibernate.hql.internal.ast.QuerySyntaxException: EdbmsEmployee is not mapped [from EdbmsEmployee edbmsEmployee where edbmsEmployee.employeeid=?]

  14. 14

    org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1

  15. 15

    org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: Apr near line 1, column 81

  16. 16

    Spring boot org.hibernate.hql.internal.ast.QuerySyntaxException: Utilisateur is not mapped (with MYSQL)

  17. 17

    Spring boot multiple module project org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped

  18. 18

    org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

  19. 19

    org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: '1.id'

  20. 20

    Spring boot org.hibernate.hql.internal.ast.QuerySyntaxException: Utilisateur is not mapped (with MYSQL)

  21. 21

    I got an org.hibernate.hql.internal.ast.QuerySyntaxException: is not mapped exception

  22. 22

    java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException MS-Sql Server

  23. 23

    org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: distinct near line 1, column 17

  24. 24

    Hibernate-Mysql QuerySyntaxException

  25. 25

    Hibernate QuerySyntaxException, Table not mapped

  26. 26

    org.hibernate.hql.ast.QuerySyntaxException:on near line 1, column 102 [FROM com.strutsProject.dto.EventClubTb

  27. 27

    Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: order near line 1, column 17 for absolutely unclear reasons

  28. 28

    org.hibernate.hql.ast.QuerySyntaxException:on near line 1, column 102 [FROM com.strutsProject.dto.EventClubTb

  29. 29

    Why does this code not throw a NullPointerException?

HotTag

Archive