Using jpa you can do it this way:
private BaseEntity findById(String table, Long id) {
Query query = entityManager.createQuery("select p from " + table
+ " p where p.id=" + id);
return (BaseEntity) query.getSingleResult();
}
private List findBaseEntity(int first, int count, String property,
boolean ascending, String table) {
Query query = entityManager.createQuery("select p from " + table
+ " p order by p.:property :sort");
query.setParameter("property", property);
if (ascending) {
query.setParameter("sort", "asc");
} else {
query.setParameter("sort", "desc");
}
query.setFirstResult(first);
query.setMaxResults(count);
return (List<BaseEntity>) query.getResultList();
}
and this in hibernate:
public Promo findPromo(Long id) {
Criteria criteria =
getHibernateSession().createCriteria(Promo.class);
criteria.add(Restrictions.eq("id", id));
return (Promo) criteria.uniqueResult();
}
public List<Promo> findPromo(int first, int count, String property,
boolean ascending, Company company) {
Criteria criteria =
getHibernateSession().createCriteria(Promo.class)
.add(Restrictions.eq("company", company));
criteria.setMaxResults(count);
criteria.setFirstResult(first);
if (ascending) {
criteria.addOrder(Order.asc(property));
} else {
criteria.addOrder(Order.desc(property));
}
return (List<Promo>) criteria.list();
}
public int promoCount(Company company) {
Criteria criteria =
getHibernateSession().createCriteria(Promo.class)
.add(Restrictions.eq("company", company));
criteria.setProjection(Projections.rowCount());
Object obj = criteria.uniqueResult();
return (Integer) obj;
}
gumnaam23 wrote:
I have done this, and it works quite nicely.
Be warned though, the biggest bottle neck in this, is the loading of the
data from database.
So for best performance, you should read only one page's worth data at a
time from your DB
(in your case 500).
If you load the whole data, and use some thing like a list iterator, you
will not get any performance
benefit.
Unfortunately ANSI SQL has no syntax for pagination of results. So you have
2 choices.
1) Keep an open DB cursor , and retrieve as much data as you want per page
rendering. But this
ties your cursor to the user session. Not a very good idea.
2) Use the vendor specific way to get paginated results. Almost all major
vendors support pagination
of query results in their own idiosyncratic way.
If you go with approach 2, you can even use the
AjaxFallbackDefaultDataTable, which will do all the
pagination etc for you, and with AJAX to boost.
You only need to provide a SortableDataProvider, which implements the size()
and iterate(int lower, int upper) calls.
Your size() call should fetch a total count of items. i.e. select count(*)
and the iterate(int lower, int upper) should retrieve the query results
between lower and upper limit.
And with sortable data provider, you even get sorting for free. (I mean the
UI, you do have to write
the back end implementation your self).
For my case, I use iBatis for DB access, so it is really easy to write DB
Vendor specific queries and
yet retain the pagination abstraction. If I need to work with another DB , I
can easily swap the
pagination query part.
Hope this has helped.
PhilipJohnson wrote:
* Finally, when I am displaying Page 1 containing 500 rows (using
something like
PageableListView), I'd like to only populate my model with rows 1-500 from
my backing
store. Let's say the user then clicks on Page 3: at that point, I'd like
to go retrieve
rows 1001-1500 from my backing store and populate my model for rows
1001-1500. How would
one go about doing this? Would it be possible to kind of sketch out the
approach for me?
--
-Wicket for love
Nino Martinez Wael
Java Specialist @ Jayway DK
http://www.jayway.dk
+45 2936 7684
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]