0

i have a query in Hibernate using criteria and the same criteria using namedQuery my question is why the named queries always used more memory than criteria(tested several times) the namedQuery is faster my shallow knowledge is when hibernate using NamedQuery they would use less inner class construction and would use less memory translation from HQL to SQL i just though that namedQuery would be more straighForward for hibernate but seems the opposite here is the small benchmarking tested several times. the result is identical in both queries.

Criteria: Clazz Test consume: 73ms... Memory Used: 3,6 MB bytes.
NamedQuery: Clazz Test consume: 39ms... Memory Used: 8,7 MB bytes.

my simple source code.. thanks a lot.

public class Test
{
  private final Class<Student>clazz=Student.class;
  public static void main(String[] args) 
  {
     Test clazz = new Test();
     clazz.startTime();
     clazz.getNamed(276);
     clazz.computeTime();//final Long memory = Runtime.getRuntime().freeMemory()-startFreeMemory;        
}
public Student get(Integer id)
{
    final Session session = .....
    Criteria criteriaById = session.createCriteria(clazz).add(Restrictions.idEq(id));
    Student byId = (Student) criteriaById.uniqueResult();
    session.close();
    return byId;
}
public Student getNamed(Integer id)
{
    final Session session = .....
    final Query query = session.getNamedQuery("namedQuery")
    .setParameter("id",id).setResultTransformer(transformer(clazz));                
    Student byId = (Student)query.uniqueResult();
    session.close();
    return byId;
}

}

chiperortiz
  • 4,751
  • 9
  • 45
  • 79
  • Is the named query native sql query or HQL? And somehow I am not convinced about the way you have calculated the memory. Time might be just fine. It would be better if you use jConsole to find out the memory usage during the process. – Abhijith Nagarajan Oct 17 '13 at 16:18
  • is just plain HQL a namedQuery – chiperortiz Oct 17 '13 at 17:24

0 Answers0