title: Hibernate HQL only hits session cache
body: <p>I am having some trouble understanding where an <code>HQL</code> query gets the information from. My project is using different threads and each thread reads/writes to the database. Threads do not share Session objects, instead I am using a <code>HibernateUtil</code> class which creates sessions for me.</p>
  
  <p>Until recently, I would only close a session after writing but not after reading. Changes to objects would be immediately seen in the database <strong>but</strong> when reading on other threads (different Session object than the one used for writing) I would get stale information. Reading and writing happened always on different threads which means different Session objects and different session caches. </p>
  
  <p>I always thought that by using <code>HQL</code> instead of <code>Criteria</code>, I would always target the database (or second level cache) and not the session cache but while debugging my code, it was made clear to me that the HQL was looking for the object in the session cache and retrieved an old outdated object.</p>
  
  <p>Was I wrong in assuming that <code>HQL</code> always targets the database? Or at least the second level cache?</p>
  
  <p>PS: I am using only one <code>SessionFactory</code> object.</p>