xiazaiyonde
2011-05-16 09:31
浏览 294
已采纳

sql报错,可是PLSQL Developer里运行没问题 。

org.hibernate.hql.ast.QuerySyntaxException: expecting OPEN, found ',' near line 1, column 14 [select OBJECT,OBJECT_KIND,MAX_CODE,sum(ACOUNT) as ACOUNT,sum(GATHER_MONEY) as GATHER_MONEY,to_char(DATE_FROM,'yyyy-mm-dd') as DATE_FROM,to_char(DATE_TO,'yyyy-mm-dd') as DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS,to_char(SUBMIT_TIME,'yyyy-mm') as SUBMIT_TIME from SDB_CON_SUBMIT where 1=1 group by SUBMIT_TIME,OBJECT,OBJECT_KIND,MAX_CODE,DATE_FROM,DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS ORDER BY SUBMIT_TIME ASC]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:80)
..........

dao的执行:
String sql = "select OBJECT,OBJECT_KIND,MAX_CODE,sum(ACOUNT) as ACOUNT," +
"sum(GATHER_MONEY) as GATHER_MONEY," +
"to_char(DATE_FROM,'yyyy-mm-dd') as DATE_FROM," +
"to_char(DATE_TO,'yyyy-mm-dd') as DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS," +
"to_char(SUBMIT_TIME,'yyyy-mm') as SUBMIT_TIME " +
"from SDB_CON_SUBMIT where 1=1 " ;

    if(null!=supplierID){
        sql+= "and FIRM_ID= "+supplierID+" ";
    }

    if(conObject!=null){
        sql+= "and object= "+""+conObject+" ";
    }

    sql +="group by SUBMIT_TIME,OBJECT,OBJECT_KIND,MAX_CODE,DATE_FROM,DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS ";
    sql +="ORDER BY SUBMIT_TIME ASC";

Query query = getSession().createQuery(sql);

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • fmjsjx 2011-05-16 09:59
    已采纳

    [quote]
    Query query = getSession().createQuery(sql);
    [/quote]
    没记错的话这行代码生成的是HQL的Query,你用SQL语句生成HQL查询当然会错,记得Hibernate也有执行SQLQuery的,好像是session.createSQLQuery(sql),不过从来没用过,具体还是参考官方文档的好。

    点赞 打赏 评论
  • shansun1234 2011-05-16 09:43

    前面的拼接没看出问题,建议LZ在
    [quote]Query query = getSession().createQuery(sql);[/quote]
    前打印出sql,然后再到pl/sql下执行看是否有语法错误。
    从错误信息上看,感觉是哪里多了个逗号。

    点赞 打赏 评论
  • iihero 2011-05-16 09:47

    [quote]select OBJECT,OBJECT_KIND,MAX_CODE,sum(ACOUNT) as ACOUNT,sum(GATHER_MONEY) as GATHER_MONEY,to_char(DATE_FROM,'yyyy-mm-dd') as DATE_FROM,to_char(DATE_TO,'yyyy-mm-dd') as DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS,to_char(SUBMIT_TIME,'yyyy-mm') as SUBMIT_TIME from SDB_CON_SUBMIT where 1=1 group by SUBMIT_TIME,OBJECT,OBJECT_KIND,MAX_CODE,DATE_FROM,DATE_TO,IT_DESC,REMARK,SUN_NUMBER,EXPRESS ORDER BY SUBMIT_TIME ASC[/quote]

    把这个弄到命令行sqlplus里执行一下,看看有没有错,是什么错。

    点赞 打赏 评论
  • shansun1234 2011-05-16 10:25

    LS的应该正解的。
    hibernate中createQuery与createSQLQuery两者区别是:
    前者用的hql语句进行查询,后者可以用sql语句查询

    点赞 打赏 评论
  • qianzhi008 2011-05-16 10:58

    createQuery是执行hql语句,而createSQLQuery才是执行sql语句,LZ显然是应该执行Query语句。

    点赞 打赏 评论

相关推荐 更多相似问题