I have a method that creates a criteria from a session
Code:
Session session = this.sessionFactory.openSession();
Criteria criteria = session.createCriteria(DsEvents.class);
adds some restrictions
and returns a POJO which has a Criteria property in it.
Then I want to use this criteria in 3 places:
First, I want to change fetch mode and add sort order and get a list from the criteria.
After that, I want to add a Projection.rowCount to get the total number of rows (on the original criteria instance, without the fetch mode and sort order).
Finally I want to get the list again with a different sor order (and extra Restrictions)
These should be 3 different methods.
When I try to pass my POJO in these methods something really strange happens: the Criteria instance inside the POJO is updated automatically.
For example, in my first method I have:
Code:
Criteria criteria = gridHelper.getCriteria();
criteria.add(Restrictions.gt("updTime", gridHelper.getLatestUpdateTime()));
criteria.addOrder(Order.asc(gridHelper.getSidx()));
return (ArrayList<T>) criteria.list();
gridHelper is the POJO instance.
in my second method, using the common POJO instance,
this line:
Code:
Criteria criteria = gridHelper.getCriteria();
does not get me the original Criteria instance,
but the one with the Restricion and the sort order form the first method!
How does this work? I do not understand how this is happening. I am not updating the criteria instance inside the POJO, why is it updated automatically?
I am probaby doing something completely wrong. Can someone help me on this? How is the correct way of doing this?