hql传参出错could not extract ResultSet

这是我写的一个方法

    public BeanAdmin adminLogin(String loginName, String loginPwd) throws Exception {
        // TODO Auto-generated method stub

        Session session = MySessionFactory.getSession();
        Transaction tx = null;
        BeanAdmin admin = null;
        try{
            Query query = session.createQuery
            ("from BeanAdmin as a where a.admin_name=:loginName and a.admin_pwd=:loginPwd");
            query.setMaxResults(1);
            query.setParameter("loginName",loginName);
            query.setParameter("loginPwd", loginPwd);
            tx = session.beginTransaction();
            admin = (BeanAdmin)query.uniqueResult();
            tx.commit();
        }catch(Exception ex){
            if(tx!=null)tx.rollback();
            ex.printStackTrace();
        }finally{
            MySessionFactory.closeSession();
        }   
        return admin;
    }

主函数

    public static void main(String args[]) throws Exception{
        AdminServiceImpl as=new AdminServiceImpl();
        BeanAdmin admin=new BeanAdmin();
        admin=as.adminLogin("sa", "123456");

    }

错误:

 log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Hibernate: select top 1 beanadmin0_.admin_id as admin_id1_1_, beanadmin0_.admin_type as admin_ty2_1_, beanadmin0_.admin_type_name as admin_ty3_1_, beanadmin0_.admin_name as admin_na4_1_, beanadmin0_.admin_pwd as admin_pw5_1_ from doudou_shop.admin beanadmin0_ where beanadmin0_.admin_name=? and beanadmin0_.admin_pwd=?
org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:91)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2065)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    at org.hibernate.loader.Loader.doQuery(Loader.java:909)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:966)
    at doudou.service.AdminServiceImpl.adminLogin(AdminServiceImpl.java:31)
    at doudou.service.AdminServiceImpl.main(AdminServiceImpl.java:153)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 beanadmin0_.admin_id as admin_id1_1_, beanadmin0_.admin_type as admin_ty2_1_, ' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:82)
    ... 17 more

3个回答

ERROR: Could not extract package's data directory. Are you sure that your
installed application is debuggable?

ndk-gdb,调试native code,出现以下错误:

ndk-gdb --verbose,查看信息:
--verbose:
Print ver......
答案就在这里:ERROR: Could not extract package's data directory. Are you sure that your installed application is d
----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

把这一行去掉试试query.setMaxResults(1);我看你生成的sql是这样的:Hibernate: select top 1 beanadmin0_.admin_id as admin_id1_1_, beanadmin0_.admin_type as admin_ty2_1_, beanadmin0_.admin_type_name as admin_ty3_1_, beanadmin0_.admin_name as admin_na4_1_, beanadmin0_.admin_pwd as admin_pw5_1_ from doudou_shop.admin beanadmin0_ where beanadmin0_.admin_name=? and beanadmin0_.admin_pwd=?
Mysql不支持top关键字,MySql中是使用limit,具体可以度娘。另附上setMaxResults和uniqueResult的区别
http://www.lihuoqing.cn/code/373.html

啊已经解决了 是因为hibernate配置里面的方言应该是Mysql

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问