We upgraded our project from Spring 3x/Hibernate 3x to Spring 4.1.5/Hibernate 4.3.8.
Initially we were using HibernateTemplate. During upgrade we removed the HibernateTemplate of spring and used Spring's declarative transaction management.
Earlier our query with
hibernateTemplate used to take very sort time to retrieve 3500 records from DB. Now when are using
query.list(), the running time is coming in minutes (4-5 mins approx).
Old CodeCode:
DAO Class:
---------
public List<LatestRecVO> getListRecs(String listId) {
HibernateTemplate ht = new HibernateTemplate(getSessionFactory());
List<LatestRecVO> listOfRecs = ht.find(" from LatestRecVO a where a.listId = ? order by a.listRecId asc", Long.valueOf(listId) );
return listOfRecs;
}
Service Class:
--------------
@Transactional
public List<LatestRecVO> getListRecs(String listId) {
List<LatestRecVO> listOfRecs = listDao.getListRecDetails(listId);
return listOfRecs;
}
New CodeCode:
DAO Class:
---------
public List<LatestRecVO> getListRecs(String listId) {
Query q = getSession().createQuery(" from LatestRecVO a where a.listId = (:listId) order by a.listRecId asc");
q.setParameter("listId", Long.valueOf(listId));
List<LatestRecVO> listOfRecs = q.list();
return listOfRecs;
}
Service Class:
--------------
@Transactional
public List<LatestRecVO> getListRecs(String listId) {
List<LatestRecVO> listOfRecs = listDao.getListRecDetails(listId);
return listOfRecs;
}
The entity class
LatestRecVO do not have any associated entity with it.
I checked the Hibernate Template's find() method and saw its uses some caching.
Tried 2nd level cache along with Query Cache but still the same result. I may have configured 2nd level cache incorrectly but to try it again i want to be sure that its the way out.
I enabled show_sql and can see it just ran a singly query. On DB the same query takes some milliseconds to run. It seems like hibernate is taking time to build objects from the result.
On one of the post it was mentioned that its mandate to have a default constructor in our entities. I have not created any arg constructor in my entity class so i assume that i do have java's default constructor in place.
Any pointer in this will be really helpful.
UpdateSorry, I cannot post the complete code here, so just giving the details. I have the composite primary key for LatestRecVO. I have created a class LatestRecPK for primary key, it implements serializable and have @Embeddable annotation. In LatestRecVO i have given @IdClass(LatestRecPK.class) to include the primary key class. LatestRecVO has a CLOB property along with String, Long and @Temporal(TemporalType.DATE) properties and corresponding setters/getters.
We ran the same query with plain JDBC, iterated resultset and formed objects in while loop. JDBC too took some 3 minutes for creating all 4k objects and adding them to a list.