初学hibernate ,今天试了一个例子但是报错
org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection available
package bijian.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sessionFactory;
static{
try{
sessionFactory=new Configuration().buildSessionFactory();
}catch(Throwable ex){
ex.printStackTrace();
}
}
private static final ThreadLocal session=new ThreadLocal();
public static Session getCurrentSession(){
Session s=(Session)session.get();
if(s==null){
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession(){
Session s=(Session)session.get();
if(s!=null){
s.close();
}
session.set(null);
}
}
最终发现是
sessionFactory=new Configuration().buildSessionFactory();
new Configuration()默认是读取hibernate.properties
new Configuration().configure()来读取hibernate.cfg.xml文件
sessionFactory=new Configuration().configure().buildSessionFactory();