Hibernate 3.3
If I’ve got code that is dynamically creating hibernate hql queries like
String query = "from Foo where ";
if( beforeDate != null ) then query+=" createdBefore < :before";
session.createQuery(query);
Does the resulting sql that will get executed on the db get cached by the SessionManager so that if the same query keeps getting executed it won’t have to get recompiled everytime or will it get re-parsed and rebuilt by hibernate every time?
I’m thinking that to increase performance of my db code I’ll have to write some static named queries in hibernate to reduce the parsing overhead if there is some.
The SQL won’t be cached, because you are creating a new Query each time. You could cache the Query, but I doubt that the parsing overhead is significant.
Some things to check for performance of that query:
createdBefore?Foohas with other objects ‘appropriate’ — that is are they lazy if possible and strict if necessary. SayFoohas a member which is loaded from theBartable. If you make the relationship lazy, you avoid a join, but if you then access theBarbelonging to eachFooyou’ve donenmore queries, and would have been better off settinglazy="false".before, and the Foo table is only updated via Hibernate, and it is not updated more often than it is queried, and the size of the result sets is not too large, you could use the query cacheAs always with any performance issue, make sure you have repeatable before and after measurements.