用Hibernate開發遇到"No CurrentSessionContextconfigured"錯誤
在使用SessionFactory的getCurrentSession方法時遇到如下錯誤,經過檢查,原因如下:
是因爲在hibernate.cfg.xml文件中忘記進行了如下設置:hibernate.current_session_context_class
如果是在web容器中運行hibernate,則在hibernate.cfg.xml中加入這句話:
<propertyname="hibernate.current_session_context_class">jta</property>
如果是在一個單獨的需要進行JDBC連接的javaapplication中運行hibernate,則這樣設置:
<propertyname="hibernate.current_session_context_class">thread</property>