当前位置: 代码迷 >> J2EE >> org.hibernate.HibernateException:Could not parse configuration/hibernate.cfg.xml解决办法
  详细解决方案

org.hibernate.HibernateException:Could not parse configuration/hibernate.cfg.xml解决办法

热度:459   发布时间:2016-04-22 01:00:49.0
org.hibernate.HibernateException:Could not parse configuration/hibernate.cfg.xml
初学hibernate配置文件都是自己写的
下面是错误信息:
org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2216)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2128)
at ch03.hibernate.Test.main(Test.java:11)
Caused by: org.dom4j.DocumentException: Error on line 2 of document : 文档中根元素前面的标记必须格式正确。 Nested exception: 文档中根元素前面的标记必须格式正确。
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2208)
... 2 more
下面是我运行的代码:
package ch03.hibernate;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
try{
SessionFactory sf=new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
 
Session session=sf.openSession();

Transaction tx=session.beginTransaction();
User user=new User();
user.setUsername("Hibernate");
user.setPassword("123");
session.save(user);
tx.commit();
session.close();
}catch(HibernateException e){
e.printStackTrace();
}

}

}
下面是配置文件hibernate.cfg.xml
<?xml version="1.0" encoding='utf-8'?>
<!DOCTYEPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-configuration>

<session-factory>

<!-- Database connection setting -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/myproject
</property>
<property name="connection.username">root</property>
<property name="connection.password">0000</property>

<!-- JDBC connection pool (use the built-in -->
<propert name="connection.pool_size">1</propert>

<property name="myeclipse.connection.profile">myproject_</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="current_session_context_class">thread</property>

<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<property name="show_sql"> true</property>

<property name="hbm2ddl.auto">create</property>

<mapping resource="ch03/hibernate/User.hbm.xml"/>

</session-factory>

</hibernate-configuration>
下面是配置文件User.hbm.xml
<?xml version="1.0" encoding='utf-8'?>
<!DOCTYEPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="ch03.hibernate.User" table="myusertalbe">
<id name="ID" 
<generator class="identity" ></generator>
</id>
<property name="Username"></property>
<property name="Password"></property>
<property name="Email"></property>
</class>

</hibernate-mapping>
下面是类User
package ch03.hibernate;

public class User {
private int id;
private String username;
private String password;
private String email;
public int getId() {
  相关解决方案