我自己写了一个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+"’试试
------解决方案--------------------
------解决方案--------------------