错误消息:线程“主”org.hibernate.HibernateException 中的异常:访问 stax 流时出错
Posted
技术标签:
【中文标题】错误消息:线程“主”org.hibernate.HibernateException 中的异常:访问 stax 流时出错【英文标题】:Error message: Exception in thread "main" org.hibernate.HibernateException: Error accessing stax stream 【发布时间】:2016-10-01 23:14:32 【问题描述】:我正在尝试使用 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>
感谢您的帮助
【问题讨论】:
【参考方案1】:我遇到了同样的问题。事实证明,系统无法从提供的 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">
对我来说效果很好。希望对您有所帮助!
【讨论】:
【参考方案2】:清除了 hibernate.cfg.xml 文件开头的空间。成功了
<?xml version="1.0" encoding="UTF-8"?>
【讨论】:
【参考方案3】:您需要关闭标签<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">
应该是
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
【讨论】:
@v.ladynev 我正在使用 spring 来配置休眠。我得到了同样的错误,但所有的表都是在最后创建的。关于如何解决这个问题的任何想法? @DeepakSunandaPrabhakar 请仔细检查XML
。或者问另一个问题。以上是关于错误消息:线程“主”org.hibernate.HibernateException 中的异常:访问 stax 流时出错的主要内容,如果未能解决你的问题,请参考以下文章