5

I´m trying to create a MySQL database table by using hibernate but I get this error message:

Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:107)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65)
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:57)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
    at com.anika.hibernate.Main.main(Main.java:18)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,34]

This answers do not solve my problem: Error connecting with database using hibernate

Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream

This is my Main.java file:

package com.anika.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;



public class Main {

    public static void main(String[] args){

    Student_Info student = new Student_Info();

    student.setName("Anika");
    student.setRollNo(1);

    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();   
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(student);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();


    }   

}

My hibernate.cfg.xml:

<?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, Relational Persistence for Idiomatic Java
  ~
  ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
  ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
  -->
<hibernate-configuration

  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernatetutorials</property>
    <property name="connection.username">root</property>
    <property name="connection.password"></property>

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

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>
    <mapping class="com.anika.hibernate.Stundent_Info"/>
  </session-factory>
</hibernate-configuration>

Thank you for your help

Community
  • 1
  • 1
Anika
  • 53
  • 1
  • 1
  • 4

4 Answers4

4

I faced the same issue. It turns out that system is not able to access the hibernate-configuration-3.0.dtd from the url provided.

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

So, I referenced it from the local system.

    <!DOCTYPE hibernate-configuration SYSTEM
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

Worked well for me. Hope it helps!

Vaibhav Bhalla
  • 971
  • 8
  • 8
1

cleared the space at the begining of hibernate.cfg.xml file. lt worked

<?xml version="1.0" encoding="UTF-8"?>

0

You need to close the tag <hibernate-configuration.

<hibernate-configuration>

There is not the ! character in the DOCTYPE:

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

Should be

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • @v.ladynev I'm using spring to configure hibernate. I am getting the same error but all the tables are being created at the end. Any idea on how to fix this? –  Jan 11 '17 at 19:03
  • @DeepakSunandaPrabhakar Just check the `XML` very carefully. Or ask another question. – v.ladynev Jan 11 '17 at 21:03
0

The issue for me was the lack of HTTPS.

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

I changed to:

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

That fixed the issue.

Bradley Davis
  • 19
  • 2
  • 7