1. getHibernateTemplate().saveOrUpdateAll(entities);
getHibernateTemplate().deleteAll(entities);
hibernate提供的批量增加和修改的方法 ,參數是實體類集合。
其實跟saveOrUpdate(Object obj) 一樣,最終操作使用的是session.saveOrUpdate(entityName, entity); 循環使用的是同一個session對象。不確定是否效率比自己寫外部循環使用saveOrUpdate要高
只要openSession一次,hibernate內部就會連接數據庫一次,所以在代碼中opesnSession的次數,越多效率越低。
1、getCurrentSession()與openSession()的區別?
* 采用getCurrentSession()創建的session會綁定到當前線程中,而采用openSession()
創建的session則不會
* 采用getCurrentSession()創建的session在commit或rollback時會自動關閉,而采用openSession()
創建的session必須手動關閉
2、使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
* 如果使用的是本地事務(jdbc事務)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事務(jta事務)
<property name="hibernate.current_session_context_class">jta</property>