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;
}
}