【问题标题】:I am getting error in: Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml我收到错误:线程“主”org.hibernate.HibernateException中的异常:无法解析配置:hibernate.cfg.xml
【发布时间】:2016-03-30 06:51:17
【问题描述】:

我得到了这个异常

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>

我的 Bean 类是:

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

我的测试员课程是:

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

我已经添加了连接到 hibernate 和 mysql 所需的所有 jar(例如 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

【问题讨论】:

  • 尝试找出hibernate.cfg.xml是否放在正确的目录中。
  • 我的 hibernate.cfg.xml 文件放在 src 文件夹中。它在任何包之外@ulrich
  • 这似乎是错误的地方......看herehere
  • 感谢@Ulrich 的帮助。但是我在更改路径后也尝试过。它不起作用。
  • 请更新您的问题并显示您尝试过的位置。这个方法调用的内容是什么:cfg.configure("...path.../hibernate.cfg.xml");?

标签: java mysql hibernate


【解决方案1】:

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

【讨论】:

  • 你在类路径中有hibernate3.jar
  • 我的类路径中有 hibernate3.jar。感谢您的帮助 @v.ladyev
  • @GourabSarkar 现在一切正常吗?你有hibernate-core-xxx.jarhibernate3.jar,还是把hibernate-core-xxx.jar 换成hibernate3.jar
  • @v.ladynev 我有两个 jars hibernate-core-3.6.9.Final jar 和 hibernate3 jar。但还是同样的问题。
  • @GourabSarkar 当然你需要删除hibernate3.jar
【解决方案2】:

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

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();
}
}

【讨论】:

  • 你提供了maven的解决方案,但是Gourab使用的是纯Eclipse
  • 我认为我们仍然可以遵循相同的结构并在 Eclipse 中尝试一个普通的 Java 项目。
  • 为了学习,混入概念不好。我不再关注这个问题了
  • 能否请你给我一个简单的java项目的解决方案。这对我有帮助。这里我不明白什么是App.java@srinivas
  • 这只是一个尝试插入Student DB和调用Hibernate Util会话工厂的主类。创建与图片相同的资源文件夹,并尝试使用下面的这个 App.java 进行插入。 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(); } }
【解决方案3】:

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中检查这个文件。

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

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

看起来你已经拥有它了。

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

【讨论】:

  • 我已经更改了文档类型。现在它也无法正常工作。我正在使用 hibernate-core 3.8.9.Final.jar。可以吗??????@v.ladynav跨度>
  • @GourabSarkar 我认为如果您使用其他 jar 的相应版本就可以了。新的DOCTYPE 的堆栈跟踪是什么?
  • 线程“主”org.hibernate.HibernateException 中的异常:无法解析配置:org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) 中的 hibernate.cfg.xml。 hibernate.cfg.Configuration.configure(Configuration.java:1425) at com.jwt.hibernate.SimpleTest.main(SimpleTest.java:12) 原因:org.dom4j.DocumentException: hibernate.sourceforge.net 嵌套异常:hibernate。 sourceforge.net at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
  • 堆栈跟踪没有变化。@v.ladynev
  • @GourabSarkar 堆栈跟踪发生了变化www.hibernate.org -> hibernate.sourceforge.net
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2017-08-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多