1.SQL VS HQL
The Hibernate SQLQuery bypasses the Hibernate Session cache and queries ONLY
against the
database. By the same token, HQL and the Hibernate Criteria queries check the
Session cache
before executing the query. If there are objects that the HQL query may
execute against
hibernate will flush the cache to the database.
Hibernate SQLQuery绕过Hibernate 会话的缓存,直接从数据库中查询;也就是说,HQL and
the Hibernate Criteria queries在执行前会先检查会话的缓存,如果缓存中存在该查询需要的
对象,hibernate会将它更新到数据库中去;
2.会话必须在一个线程使用,因为它不是线程安全的;
Sessions should be used by only one application thread at a time. This is a
common concern in web applications, which are multithreaded by their very
nature.
所以不要将在session1中取出来的东西,在session2中调用saveOrUpdate方法,会抛异常;
如果你必须要这样做的话,请使用save或者update方法,这样hibernate就不用通过判断来调用save或者update方法了。
http://i-proving.ca/space/Technologies/Hibernate/SQL+vs+HQL+with+the+Session+Cache
http://www.developer.com/open/article.php/10930_3559931_4