错误消息:线程“main”中的异常org.hibernate.HibernateException:访问stax流时出错

时间:2016-06-02 09:39:54

标签: java mysql eclipse hibernate

我正在尝试使用hibernate创建MySQL数据库表但是我收到此错误消息:

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]

这个答案无法解决我的问题:Error connecting with database using hibernate

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

这是我的Main.java文件:

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


    }   

}

我的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>

感谢您的帮助

3 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。事实证明,系统无法从提供的URL访问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">

所以,我从本地系统引用它。

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

为我工作得很好。希望它有所帮助!

答案 1 :(得分:0)

您需要关闭代码object->on_event_foo(/* .. */)

<hibernate-configuration

<hibernate-configuration>中没有!字符:

DOCTYPE

应该是

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

答案 2 :(得分:0)

清除了hibernate.cfg.xml文件开头的空格。起作用了

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