-->
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.  [ 3 posts ] 
Author Message
 Post subject: Pagination with Criteria results across Sessions
PostPosted: Mon Aug 25, 2008 8:06 am 
Newbie

Joined: Mon Aug 25, 2008 7:45 am
Posts: 2
Hi,
I though I'd be able to find the answer to this, but after lots of searching I'm still scratching my head.
I'm using a Criteria to return lots of rows, so I'm using setFirstResult & setMaxResults to retrieve 100 results per user request.
If I do this in a single Session as a test, it's working fine. However when I try to implement session-per-request and reattach the criteria in the next user request in a new session, I hit problems. I'm assuming I don't have to re-execute the database query - I can use the previous criteria in the next session to retrieve the next 100 rows.

My problem in a nutshell is - how do I reattach the criteria in the next session.

My (abbreviated) code is as follows :-

Code:
      public void getFirstPage()
      {
            Session session = DBUtils.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            Criteria crit = session.createCriteria(UserVO.class);
            crit.add( Expression.eq(........));

            crit.setMaxResults(100);
            crit.setFirstResult(0);
            myBean.setCriteria(crit);

            result = crit.list();
            session.getTransaction().commit();
      }

      public void getNextPage(int startRow)
      {
            Session session = DBUtils.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            Criteria crit = myBean.getCriteria();

            session.lock(crit,LockMode.NONE);  // #### exception here

            crit.setMaxResults(100);
            crit.setFirstResult(startRow);

            result = crit.list();
      }



I get the following exception when I try to reattach the criteria object from the first session (which was stored in a bean)

Code:
org.hibernate.MappingException: Unknown entity: org.hibernate.impl.CriteriaImpl
   at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
   at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
   at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:50)
   at org.hibernate.impl.SessionImpl.fireLock(SessionImpl.java:584)
   at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:576)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
   at $Proxy0.lock(Unknown Source)
   at myprog.UserDAO.getNextPage(UserDAO.java:366)


I'm thinking that reattaching the Criteria object isn't the way to do this, but repeated calls to crit.setFirstResult(startRow); & crit.list() works fine in the same session (I've tried storing this in the HTTPSession as a test)

Any help would be gratefully appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 8:19 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I think you need to use a DetachedCriteria. See http://www.hibernate.org/hib_docs/v3/ap ... teria.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 25, 2008 2:58 pm 
Newbie

Joined: Mon Aug 25, 2008 7:45 am
Posts: 2
Thanks very much nordborg, I've replaced the Criteria with a DetachedCriteria and it's working fine now.

In case anyone else is trying to do this, the code looks like :-

Code:
      public void getFirstPage()
      {
            Session session = DBUtils.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            DetachedCriteria crit = DetachedCriteria.forClass(UserVO.class);
            crit.add( Expression.eq(.....));
            myBean.setCriteria(crit); // store criteria for nextpage request
           
            List result = crit.getExecutableCriteria(session)
                              .setMaxResults(100)
                              .setFirstResult(0)
                              .list();

            session.getTransaction().commit();
      }

      public void getNextPage(int startRow)
      {
            Session session = DBUtils.getSessionFactory().getCurrentSession();
            session.beginTransaction();

            DetachedCriteria crit = myBean.getCriteria();
            List result = crit.getExecutableCriteria(session)
                              .setMaxResults(100)
                              .setFirstResult(startRow)
                              .list();

            session.getTransaction().commit();
      }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.