views:

117

answers:

1

I have a paging worked with NHibernate:

public IEnumerable<Answer> GetAnswers(int page, int pageSize)
    {
        return HibernateTemplate.Execute(
           session =>
           {
               var criteria = session.CreateCriteria(typeof(Answer));
               return criteria
                   .SetFirstResult((page - 1) * pageSize)
                   .SetMaxResults(pageSize).List<Answer>();
           }
           );
    }

Now my task is to determine on which page concrete Answer object is placed. How can i do it? Does nhibernate provide some info like index of row in result set?

+1  A: 

NHibernate HQL offers an index function for this purpose.

public int GetRowIndex(Answer answer)
{
    return HibernateTemplate.Execute(
       session =>
       {
           return session.CreateQuery(
                "select index(a) from Answer a").UniqueResult<int>();
       });
}

I haven't tested the code, but you can get more information from the NHibernate doc online (outdated version): https://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_single/#queryhql-expressions

Guillaume Poirier