-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to query page by page with Hibernate
PostPosted: Sun Jan 31, 2010 2:45 am 
Beginner
Beginner

Joined: Mon Feb 02, 2009 3:22 am
Posts: 26
Hi,

I am using Struts+Spring+Hibernate.
I have a MySQL table tb_ext, the pesistent object is TbExt as following:

public class TbExt implements java.io.Serializable
{
// Fields
private Long extId;
private String extNumber;
private String extBox;
private String extStatus;
private String extUser;
private String extPassword;
private String extPrivateip;
private String extDescription;
...
}

I'd like to do some query like sql string " SELECT * FROM tb_ext LIMIT 5,10" , so I write a method in DAO:

public List queryAll(int currentPage, int pageSize ) throws Exception
{
String queryString = "FROM TbExt limit " + (currentPage - 1) * pageSize + "," + pageSize;
System.out.println("queryAll=" +queryString);
log.debug("query TbExt page by page");
try
{
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re)
{
log.error("find all failed", re);
throw re;
}
}

But i got error:

queryAll=FROM TbExt limit 0,20
org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: 0
near line 1, column 43 [FROM com.hq.ictbox.hi.mapping.TbExt limit 0,20]; nested
exception is org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 0 nea
r line 1, column 43 [FROM com.hq.ictbox.hi.mapping.TbExt limit 0,20]
Caused by: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: 0 near
line 1, column 43 [FROM com.hq.ictbox.hi.mapping.TbExt limit 0,20]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxExcepti


I searched the forum and some answer says "Limit was never a supported clause in HQL. You are meant to use setMaxResults()", but how to control the current page number when setMaxResults() is used?


Thanks.


Top
 Profile  
 
 Post subject: Re: How to query page by page with Hibernate
PostPosted: Sun Jan 31, 2010 6:33 am 
Newbie

Joined: Sat Jan 30, 2010 6:17 pm
Posts: 5
You can use native SQL queries if you really decide to do things such way.

Code:
getHibernateTemplate().executeFind(
                new HibernateCallback()
                {
                    public Object doInHibernate( Session session )
                            throws HibernateException, SQLException
                    {
                        List l = session.createSQLQuery(
                                "SELECT  {tbExt} FROM tb_ext {tbExt} LIMIT 5,10" )
                                .addEntity( "tbExt", TbExt.class)
                                .list();
                        return l;
                    }
                });


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.