我的hibernate.cfg.xml文件如下:
<?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"> <hibernate-configuration> <session-factory name=""> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.autocommit">true</property> <property name="connection.useUnicode">true</property> <property name="connection.characterEncoding">UTF-8</property> <property name="hibernate.current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/main/News.hbm.xml"/> </session-factory> </hibernate-configuration>
其中紅色字體的部分導致的異常。
若<session-factory>配置了name屬性,卻沒有配置JNDI,則會報錯。
解決方案:刪除name屬性 或者 配置JNDI