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.