简体   繁体   English

我在以下地方遇到错误:线程“ main” org.hibernate.HibernateException中的异常:无法解析配置:hibernate.cfg.xml

[英]I am getting error in: Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml

I am getting this Exception 我收到此异常

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)

My hibernate.cfg file is 我的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>

My Mapping File is: 我的映射文件是:

<?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 class is: 我的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 class is: 我的测试人员课程是:

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 我的文件夹结构是

I have added all required jars for connecting to hibernate and mysql(eg hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar) But still I am getting the error. 我已经添加了连接到hibernate和mysql所需的所有jar(例如,hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar),但仍然出现错误。 Please Help me out. 请帮帮我。 Thanks in advance 提前致谢

Full Stack Trace: 全栈跟踪:

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 我的jar文件是

I copied every file from your page as above and created a new project. 我如上所述从您的页面复制了每个文件,并创建了一个新项目。 Please follow the below structure to avoid the above exception. 请遵循以下结构以避免上述异常。

放置休眠CFG文件的项目结构

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

Hibernate can't parse your hibernate.cfg.xml . Hibernate无法解析您的hibernate.cfg.xml

hibernate-core-3.6.9.Final.jar has a file with DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd hibernate-core-3.6.9.Final.jar的文件带有DTD hibernate-core-3.6.9.Final/org/hibernate/hibernate-configuration-3.0.dtd

Check this file inside hibernate-core-3.6.9.Final.jar . hibernate-core-3.6.9.Final.jar检查此文件。

You need to have the same DTD as hibernate-configuration-3.0.dtd has. 您需要具有与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">

Looks like you already have it. 看起来您已经拥有了。

May be you have other hibernate-core jar in the class path with other DTD. 可能是您在类路径中与其他DTD一起使用了其他hibernate-core jar。

Could you try to check your .jar again? 您可以尝试再次检查您的 .jar吗? I try to execute your project SimpleTest.java with my .jar . 我尝试使用.jar执行您的项目SimpleTest.java It is work. 是工作 Please take a look 请看一下

冬眠的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主要” org.hibernate.HibernateException中的异常:无法解析配置:hibernate.cfg.xml - Exception in thread “main” org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml “线程“ main”中的异常org.hibernate.HibernateException:无法解析配置:hibernate.cfg.xml” - “Exception in thread ”main“ org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml” org.hibernate.HibernateException:无法解析配置:hibernate.cfg.xml - org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml 线程“ main” org.hibernate.HibernateException中的异常:找不到hibernate.cfg.xml - Exception in thread “main” org.hibernate.HibernateException: hibernate.cfg.xml not found 线程“主” org.hibernate.HibernateException中的异常:无法解析 - Exception in thread “main” org.hibernate.HibernateException: Could not parse org.hibernate.HibernateException:/hibernate.cfg.xml找不到 - org.hibernate.HibernateException: /hibernate.cfg.xml not found 无法创建会话工厂。org.hibernate.HibernateException:无法解析配置:/hibernate.cfg.xml - Session Factory could not be created.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml 运行简单的Hibernate应用程序时出现HibernateException“无法解析配置:hibernate.cfg.xml”错误 - HibernateException “Could not parse configuration: hibernate.cfg.xml” error while running a simple Hibernate Application 线程“ main” org.hibernate.HibernateException中的异常: - Exception in thread “main” org.hibernate.HibernateException: Hibernate异常:无法解析配置:hibernate.cfg.xml - Hibernate Exception : Could not parse configuration: hibernate.cfg.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM