0

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

<?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:

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(e.g hibernate-core 3.8.9.Final.jar,mysql-connector-java-5.1.12-bin.jar) But still I am getting the error. 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

Gourab Sarkar
  • 80
  • 2
  • 14

3 Answers3

0

I copied every file from your page as above and created a new project. Please follow the below structure to avoid the above exception.

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();
}
}
srinivas
  • 474
  • 7
  • 14
  • you provide a solution for maven, but Gourab uses pure Eclipse – JimHawkins Mar 30 '16 at 07:37
  • I think we can still follow the same structure and try for a normal Java project in Eclipse. – srinivas Mar 30 '16 at 07:40
  • for learning, it's not good to mix up to concepts. I don't follow this question anymore – JimHawkins Mar 30 '16 at 07:42
  • Could you please give me a solution for simple java project.It will be helpful for me.Here I am not understanding what is App.java@srinivas – Gourab Sarkar Mar 30 '16 at 07:43
  • It is just a main class to try for insertion of Student DB and to call session factory of Hibernate Util. Create the resources folder as same in the picture and try to use this App.java below for inserting. `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(); } }` – srinivas Mar 30 '16 at 07:46
  • _I think we can still follow the same structure and try for a normal Java project in Eclipse._ It is incorrect. Because of Maven builder adds `src/main/resources` to the class path. – v.ladynev Mar 30 '16 at 07:46
  • Please, add your example to the answer :) – v.ladynev Mar 30 '16 at 07:47
  • Could you please tell me what is the problem in using normal java project and what is the mistake here I did.@srinivas @v.ladynev – Gourab Sarkar Mar 30 '16 at 07:49
  • @GourabSarkar You can use normal Java project. It is correct :) – v.ladynev Mar 30 '16 at 07:50
  • But still I am getting the same error.Could you please tell me the reason.where I am doing mistake @ v.ladynev – Gourab Sarkar Mar 30 '16 at 07:52
  • @GourabSarkar Please, add full stack trace to your question :) – v.ladynev Mar 30 '16 at 07:52
  • Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /resources/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 – Gourab Sarkar Mar 30 '16 at 07:54
  • Now I have added hibernate.cfg file in resources folder. – Gourab Sarkar Mar 30 '16 at 07:56
  • @GourabSarkar To your question! And full. – v.ladynev Mar 30 '16 at 07:57
  • Can You post the SimpleTest.java once. @GourabSarkar – srinivas Mar 30 '16 at 07:59
  • @GourabSarkar Hibernate can find `hibernate.cfg.xml` but can't parse it :) – v.ladynev Mar 30 '16 at 08:00
  • Try this , download hibernate-core jar from maven repository and check again. – srinivas Mar 30 '16 at 09:53
0

Hibernate can't parse your 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

Check this file inside hibernate-core-3.6.9.Final.jar.

You need to have the same DTD as hibernate-configuration-3.0.dtd has.

<!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.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • I have changed the doctype.Now also Its not working.I am working with hibernate-core 3.8.9.Final.jar.Is it fine ??????@v.ladynav – Gourab Sarkar Mar 30 '16 at 08:10
  • @GourabSarkar I think it is fine if you use corresponding versions of other jars. What the stack trace with new `DOCTYPE`? – v.ladynev Mar 30 '16 at 08:12
  • 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: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) – Gourab Sarkar Mar 30 '16 at 08:12
  • No changes in stack trace.@v.ladynev – Gourab Sarkar Mar 30 '16 at 08:14
  • @GourabSarkar There is a change in the stack trace `www.hibernate.org` -> `hibernate.sourceforge.net` – v.ladynev Mar 30 '16 at 08:15
  • oh.Sorry I didn't notice.@v.ladynev – Gourab Sarkar Mar 30 '16 at 08:16
  • @GourabSarkar Sorry I was mistaken. You had the correct DTD. I update my post with some thought. – v.ladynev Mar 30 '16 at 08:33
  • @GourabSarkar Try to use Hibernate 4 or Hibernate 5 – v.ladynev Mar 30 '16 at 08:42
0

Could you try to check your .jar again? I try to execute your project SimpleTest.java with my .jar. It is work. Please take a look

hibernate

Mizuki
  • 2,153
  • 1
  • 17
  • 29