我收到错误:线程中出现异常" main" org.hibernate.HibernateException:无法解析配置:hibernate.cfg.xml

时间:2016-03-30 06:51:18

标签: java mysql hibernate

我收到此例外

Exception in thread "main" org.hibernate.HibernateException: Could not parse      configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:13)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)

我的hibernate.cfg文件是

<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3006/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property  name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property> 
<mapping resource="com/jwt/hibernate/student.hbm.xml" />
</session-factory>
</hibernate-configuration>

我的映射文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.jwt.hibernate.Student" table="STUDENT">
<id column="ID" name="id" type="int" />
<property column="STUDENT_NAME" name="name" type="string" />
<property column="DEGREE" name="degree" type="string" />
<property column="ROLL" name="roll" type="string" />
<property column="PHONE" name="phone" type="string" />
</class>
</hibernate-mapping>

My Bean课程是:

public class Student {
    private int id;
    private String name;
    private String degree;
    private String roll;
    private String phone;
    /** Getters and setters omitted **/
}

My Tester课程是:

public class SimpleTest {

  public static void main(String[] args) {

    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Student student = new Student();
    student.setName("Gourab");
    student.setRoll("101");
    student.setPhone("8888");
    student.setDegree("B.E");

    Transaction tx = session.beginTransaction();
    session.save(student);
    System.out.println("Object saved successfully.....!!");
    tx.commit();
    session.close();
    factory.close();
  }
}

My Folder Structure is

我添加了所有必需的jar用于连接到hibernate和mysql(例如hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar) 但我仍然得到错误。 请帮帮我。 提前致谢

完整堆栈跟踪:

Exception in thread "main" org.hibernate.HibernateException: Could not parse    configuration: hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:12)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
... 2 more

My jar files are

3 个答案:

答案 0 :(得分:0)

我如上所述复制了您网页中的每个文件并创建了一个新项目。请遵循以下结构以避免上述异常。

Project Structure to place hibernate cfg file

public class App 
{
public static void main( String[] args )
{
    System.out.println("Maven + Hibernate + MySQL");
    Session session = HibernateUtil.getSessionFactory().openSession();

    session.beginTransaction();
    Student st1 = new Student();

    st1.setName("srinivas");
    st1.setPhone("99");
    st1.setRoll("123");


    session.save(st1);
    session.getTransaction().commit();
}
}

答案 1 :(得分:0)

Hibernate无法解析您的hibernate.cfg.xml

hibernate-core-3.6.9.Final.jar有一个DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd

的文件

hibernate-core-3.6.9.Final.jar内查看此文件。

您需要与DTD具有相同的hibernate-configuration-3.0.dtd

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

看起来你已经拥有它了。

您可能在其他DTD的类路径中有其他hibernate-core jar。

答案 2 :(得分:0)

您可以尝试再次检查 .jar吗?我尝试使用SimpleTest.java执行您的项目.jar。这是工作。请看一下

hibernate