query.setFirstResult解析

原文:http://blog.csdn.net/zj972535075/article/details/50489451


package com.zyf.test;


import java.text.ParseException;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.zyf.model.Student;

public class Test {
    
    public static void main(String[] args) throws ParseException {
        Configuration cfg = new Configuration().configure();

        SessionFactory factory = cfg.buildSessionFactory();
        
        int pageNum = 2;
        int size = 2;
        Session session = null;
        Transaction tx = null;

        try {
            session = factory.openSession();
            tx = session.beginTransaction();
            
            Query query = session.createQuery("select s from Student as s where name like :name and s.sex like :sex");
            query.setParameter("name", "%I%");
            query.setParameter("sex", "%Men%");
            query.setFirstResult(3);//表示的是从查询记录的地几个开始,而不是从第几页开始
            //query.setFirstResult((pageNum-1)*size);这才是正确的偏移量
            query.setMaxResults(2);
            
            List list  = query.list();
            
            System.out.println("list size :" + list.size());
            for(int i = 0; i < list.size(); i++) {
                System.out.println(((Student)list.get(i)).getId()+":"+((Student)list.get(i)).getSex());
            }
            
            
            
            tx.commit();
            
        

        } catch (Exception e) {
            if (null != tx) {
                tx.rollback();
            }
            e.printStackTrace();
        }
        
        finally {
            if (null != session) {
                session.close();
            }

        }

    }
}
发布了306 篇原创文章 · 获赞 193 · 访问量 71万+
展开阅读全文

hibernate使用query.setMaxResults()和query.setFirstResult()进行对mysql的数据进行分页查询

12-14

我在使用hibernate进行对mysql的数据进行分页查询的时候,用到执行到query.setMaxResults()的时候,报错误 ![图片说明](https://img-ask.csdn.net/upload/201712/15/1513271834_696847.png) 报错: Hibernate: select top 4 goods0_.id as id5_, goods0_.supplierId as supplierId5_, goods0_.bigCategoryId as bigCateg3_5_, goods0_.smallCategoryId as smallCat4_5_, goods0_.goodsBianhao as goodsBia5_5_, goods0_.name as name5_, goods0_.synopsis as synopsis5_, goods0_.unitprice as unitprice5_, goods0_.brand as brand5_, goods0_.factoryProduction as factory10_5_, goods0_.purchasePrice as purchas11_5_, goods0_.chanpinxilie as chanpin12_5_, goods0_.chanpinleixing as chanpin13_5_, goods0_.gonglv as gonglv5_, goods0_.guigechicun as guigech15_5_, goods0_.yingyongkongjian as yingyon16_5_, goods0_.shiyongmianji as shiyong17_5_, goods0_.chanpinfengge as chanpin18_5_, goods0_.chanpingongneng as chanpin19_5_, goods0_.sewen as sewen5_, goods0_.xiansezhishu as xiansez21_5_, goods0_.caizhi as caizhi5_, goods0_.zhibao as zhibao5_ from ruilinxitong.goods goods0_ where 1=1 and goods0_.smallCategoryId=? 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 com.ruilin.dao.impl.GoodsDaoImpl.getAlumByGidAndTitle(GoodsDaoImpl.java:54) at com.ruilin.dao.impl.GoodsDaoImpl.main(GoodsDaoImpl.java:86) Caused by: com.mysql.jdbc.exceptions.jdbc4.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 '4 goods0_.id as id5_, goods0_.supplierId as supplierId5_, goods0_.bigCategoryId ' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) at com.mysql.jdbc.Util.getInstance(Util.java:408) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966) 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) ... 9 more 现在该如何解决呀 很急的问题,帮忙解答,万分感谢 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览