运行测试代码报错:
Caused by: org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
我的配置信息
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property><!--不写的话不会自动生成表 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Hibernate_Annotation</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="myeclipse.connection.profile">MySqlDriver</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping class="ray.one2one.Woman" />
<mapping class="ray.one2one.Man" />
</session-factory>
</hibernate-configuration>发现配置没有问题,查看帮助文档,发现以前获取sessonFactory的方法已经过时了,推荐了另外一种方法,修改后成功
public class SessionUtil {
public static Session getSession() {
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry= new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry);
return sessionFactory.openSession();
}
本文介绍了一个Hibernate连接异常的问题,配置文件中已正确设置了数据库连接参数和方言等信息,通过更新SessionFactory创建方式解决了“Connection cannot be null when 'hibernate.dialect' not set”的异常。
5148

被折叠的 条评论
为什么被折叠?



