1. 程式人生 > >No CurrentSessionContext configured!" 異常解決方案

No CurrentSessionContext configured!" 異常解決方案

hibernate 老說沒有配方言

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
這句話明明就寫在了配置檔案裡面,可老是沒有寫
錯誤如下:
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set

name改成"hibernate.dialect"也不行

Hibernate-Version: 3.1.1
mysql -Verison: 4.0.16


用這種方法正確
static {

try {
configuration = new Configuration();

sessionFactory = configuration.configure().buildSessionFactory();
} catch (Throwable ex) {

throw new ExceptionInInitializerError(ex);
}
}

這種方式會出現異常
// static {
// try {
// // Create a configuration based on the properties file we've put
// Configuration config = new Configuration();
// config.addClass(Customer.class).addClass(Order.class);
// // Get the session factory we can use for persistence
// sessionFactory = config.buildSessionFactory();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }

第二種方式是針對使用properties檔案配置hiernate的寫法,使用hibernate.cfg.xml應使用第一種呼叫方式

或者在hibernate.cfg.xml中加入:

<property name="current_session_context_class">thread</property>