Unlabeled ordinal parameter ('?' rather than ?1)




Asked on November 26, 2023
Hi, I have a HQL query.
public boolean itemExists(String title, String category) {
String hql = "FROM Items as itm WHERE itm.title = ? and itm.category = ?";
int cnt = entityManager.createQuery(hql)
             .setParameter(1, title)
             .setParameter(2, category)
             .getResultList()
             .size();
return cnt > 0 ? true : false;
}

When I run itemExists method, it throws error.

org.hibernate.query.SemanticException: Unlabeled ordinal parameter ('?' rather than ?1)
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPositionalParameter(SemanticQueryBuilder.java:3854) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPositionalParameter(SemanticQueryBuilder.java:3847) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitPositionalParameter(SemanticQueryBuilder.java:253) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.grammars.hql.HqlParser$PositionalParameterContext.accept(HqlParser.java:11077) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.query.hql.internal.SemanticQueryBuilder.visitParameterExpression(SemanticQueryBuilder.java:3823) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.grammars.hql.HqlParser$ParameterExpressionContext.accept(HqlParser.java:7514) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visitChildren(AbstractParseTreeVisitor.java:46) ~[antlr4-runtime-4.10.1.jar:4.10.1]
at org.hibernate.grammars.hql.HqlParserBaseVisitor.visitBarePrimaryExpression(HqlParserBaseVisitor.java:720) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]
at org.hibernate.grammars.hql.HqlParser$BarePrimaryExpressionContext.accept(HqlParser.java:7064) ~[hibernate-core-6.2.7.Final.jar:6.2.7.Final]




Replied on November 26, 2023
You can solve your problem in two ways.

[1]. Positional Parameters

Since Hibernate 5.3, syntax of positional Parameters has changed.
In HQL, replace (?) with numbers such as 
(?1) for first position,
(?2) for second position
(?3) for third position and so on.

You HQL will be as given below.

FROM Items as itm WHERE itm.title = ?1 and itm.category = ?2

You complete method code will look as below.

@Override
public boolean itemExists(String title, String category) {

String hql = "FROM Items as itm WHERE itm.title = ?1 and itm.category = ?2";

int count = entityManager.createQuery(hql)
                      .setParameter(1, title)
             .setParameter(2, category)
                      .getResultList()
                      .size();
return count > 0 ? true : false;
}


[2]. Named parameters

Use named parameters as below.

public boolean itemExists(String title, String category) {
       String hql = "FROM Items as itm WHERE itm.title = :title and itm.category = :category";
      int cnt = entityManager.createQuery(hql)
             .setParameter("title", title)
             .setParameter("category", category)
             .getResultList()
             .size();
      return cnt > 0 ? true : false;
}




Replied on November 26, 2023
Both solutions are working. Thanks.

Write Answer










©2024 concretepage.com | Privacy Policy | Contact Us