HQL 查询中的意外标记错误
Unexpect Token Error in HQL Query
我在使用以下代码时遇到问题,该代码旨在使用 HQL 从数据库中提取唯一对象。
public class VipFeeStructureFacade {
private static String QUERY_BASED_ON_DISPLAY_NAME ="from VipFeeStructure v where v.displayName = ";
private String createQueryOnDisplayName(String displayName) {
String query = "";
query = displayName;
return QUERY_BASED_ON_DISPLAY_NAME + query;
}
public VipFeeStructure getVipFeeStructureFromDisplayName(String displayName) {
return (VipFeeStructure)FacadeTools.executeHQLQueryForUniqueObject(createQueryOnDisplayName(displayName));
}
}
returns相同信息的SQL代码为:
select * from Vip_Fee_Structure v where v.display_name = "..."
这运行正常,但是 运行 getVipFeeStructureFromDisplayName()
returns 出现以下错误:
org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:68: unexpected token: m
Feb 13, 2015 10:29:30 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:68: unexpected token: m
line 1:68: unexpected token: m
at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:668)
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2721)
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2677)
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2546)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2462)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2427)
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2137)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1913)
at org.hibernate.hql.internal.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:474)
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:729)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:316)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:179)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at sun.reflect.GeneratedMethodAccessor419.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy0.createQuery(Unknown Source)
知道是什么原因造成的吗?
您应该将 "parameter" 括在单引号中。
例如,如果 displayName
是 myName
,查询将看起来像 where v.displayName = myName
,而它应该是 where v.displayName = 'myName'
。
无论如何,您应该使用实际参数而不是字符串连接。
我在使用以下代码时遇到问题,该代码旨在使用 HQL 从数据库中提取唯一对象。
public class VipFeeStructureFacade {
private static String QUERY_BASED_ON_DISPLAY_NAME ="from VipFeeStructure v where v.displayName = ";
private String createQueryOnDisplayName(String displayName) {
String query = "";
query = displayName;
return QUERY_BASED_ON_DISPLAY_NAME + query;
}
public VipFeeStructure getVipFeeStructureFromDisplayName(String displayName) {
return (VipFeeStructure)FacadeTools.executeHQLQueryForUniqueObject(createQueryOnDisplayName(displayName));
}
}
returns相同信息的SQL代码为:
select * from Vip_Fee_Structure v where v.display_name = "..."
这运行正常,但是 运行 getVipFeeStructureFromDisplayName()
returns 出现以下错误:
org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:68: unexpected token: m
Feb 13, 2015 10:29:30 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:68: unexpected token: m
line 1:68: unexpected token: m
at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:668)
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2721)
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2677)
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2546)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2462)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2427)
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2137)
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1913)
at org.hibernate.hql.internal.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:474)
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:729)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:316)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:179)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at sun.reflect.GeneratedMethodAccessor419.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy0.createQuery(Unknown Source)
知道是什么原因造成的吗?
您应该将 "parameter" 括在单引号中。
例如,如果 displayName
是 myName
,查询将看起来像 where v.displayName = myName
,而它应该是 where v.displayName = 'myName'
。
无论如何,您应该使用实际参数而不是字符串连接。