views:

363

answers:

0

I`m doing pagination, 2nd level cache is set up. Through hibernate+criteria it works fine, but through jpa it works much more slower.

           final String hql = "from Employee e "
               + "left join fetch e.positionedEmployees as p "
               + "left join fetch p.address as a "
               + "left join fetch p.company as c "
               + "left join fetch a.city as city "
               + "left join fetch city.country as country "
               + "order by e.id";

return (List<Employee>) getJpaTemplate().execute(new JpaCallback() {

           public Object doInJpa(EntityManager em) throws PersistenceException {
               Query query = em.createQuery(hql);
               query.setFlushMode(FlushModeType.COMMIT); //synchronize with db
               query.setHint("org.hibernate.cacheable", true);//2nd level cache
               query.setFirstResult(startNumber);
               query.setMaxResults( numberToRetrieve);
               List result = query.getResultList();
               return result;
           }
       });

Problems: 1) jpa pagination much more slower than in Hiber + criteria 2) request is cached, ie new requests are taken from the cache. In hiber, too, is a cache, but there it works, if I take the page, which i already took. In jpa cache always work (do not understand where it already has a query with parameters!) will be happy to answer). Under settings, I mean startNumber and numberToRetrieve.

Disabled this line:

query.setHint("org.hibernate.cacheable", true);//2nd level cache

Now the requests are not cached, but works much slower than Hibernate + criteria.

Thanks in advance!