Hibernate Dialect must be explicitly set

在偶然一次运行hibernate测试类的时候,出现如下错误,Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set,但是我明明在配置文件中配置了啊?
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
这是因为我在代码中使用了这种方式
SessionFactory sessionFactory = new Configuration().buildSessionFactory();
来获取sessionFactory,但是此种方式并不适用于使用xml配置文件的方式,所以将其改为 
new Configuration().configure().buildSessionFactory();即可,在调用configure()方法的时候,我们深入源码可以看到

 public Configuration configure()throws HibernateException
{
configure("/hibernate.cfg.xml");
return this;
}
该方法调用了使用默认名称的xml配置文件,所以就可以读取到了。
原文地址:https://www.cnblogs.com/james1207/p/3293888.html