日期:2014-05-20  浏览次数:20872 次

hibernate出了点莫名其妙的问题,请教了
我自己写了一个hql的语句,然后放入dao类中,具体代码如下
Java code
public List<Temprelation> findMaxCount(String ASId){
        log.debug("find the Max Count Object "+ASId+" !");
        String queryString = " from Temprelation t where t.id.songinfo.sid = "+ASId+" order by t.count desc";
        Query queryObject = null;
        try {
                queryObject = getSession().createQuery(queryString);
            } catch (RuntimeException e) {
            log.error("find the Max Count Object "+ASId+" is failed!",e);
            throw e;
        }
        System.out.println("size= "+queryObject.list().size());
        List<Temprelation> temp  = queryObject.list();
        List<Temprelation> list;
        if(temp.size()>=2)
            list = temp.subList(0,2);
        else
            list = temp;
        return list;
        }


在我调用这个方法的时候,出现了点问题,数据库中的sid都是40000001,40000002,40000003,这样的字符串类型,当我赋值参数40000001,40000002这些数据库中存在的字符串时,能正常调用,当我赋值参数由阿拉伯数字组成的字符串的时候,虽然数据库中不存在该主键,返回的List的长度为0,这也正常。可是,当我赋值的参数中有一个字母加阿拉伯数字组成的字符串,或者赋值参数为全字母的字符串,myeclipse就直接报错了,报错的如下
Java code
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    at org.hibernate.loader.Loader.doList(Loader.java:2235)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2124)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at HIBER.TemprelationDAO.findMaxCount(TemprelationDAO.java:77)
    at MySessionClass.TotalInfoBiz.GetMAXRelation(TotalInfoBiz.java:67)
    at MySessionClass.test2.main(test2.java:26)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown column 'fddfd' in 'where clause'
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
    at org.hibernate.loader.Loader.doQuery(Loader.java:697)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2232)
    ... 10 more



调试了半天,搞的我很郁闷,请大家帮一下忙。谢谢

------解决方案--------------------
没用过 ‘"+ASId+"’试试
------解决方案--------------------
探讨
没用过 ‘"+ASId+"’试试

------解决方案--------------------
探讨

myeclipse显示报错的语句正是try{}中的 queryObject = getSession().createQuery(queryString);这一句。我都加了try{}catch{}了还是会出现这样的报错,我百思不得其解啊。
还有报错语句Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Unknown ……