正确决解Hibernate4.*中: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://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
    <property name="connection.url">
        jdbc:mysql://localhost/myuser
    </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="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="myeclipse.connection.profile">
        MySqL JDBC Driver
    </property>
    <mapping resource="domain/News.hbm.xml" />
</session-factory>

</hibernate-configuration>

 

SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().buildServiceRegistry());

替换成

SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry());

还需要在hibernate.cfg.xml中加入一条配置:

<property name="javax.persistence.validation.mode">none</property>

posted on 2013-09-27 23:00  我竟然比我还帅  阅读(252)  评论(0编辑  收藏  举报

导航