HQL查询中出现意外的令牌错误

时间:2015-02-13 16:43:36

标签: java hibernate hql

我遇到以下代码的问题,意味着使用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));
    }
}

返回相同信息的SQL代码是:

select * from Vip_Fee_Structure v where v.display_name = "..."

并且运行正常,但运行getVipFeeStructureFromDisplayName()会返回以下错误:

    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)

知道可能导致这种情况的原因吗?

1 个答案:

答案 0 :(得分:3)

您应该将“参数”括在单引号中。

例如,如果displayNamemyName,则查询将显示为where v.displayName = myName,而应该是where v.displayName = 'myName'

无论如何,你应该使用真实参数而不是字符串连接。