java 无法解析hibernate.cfg.xml

jm81lzqq  于 2023-06-20  发布在  Java
关注(0)|答案(3)|浏览(139)

我无法解析hibernate.cfg.xml并获取

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.hibernate.org/xsd/orm/cfg", local:"hibernate-configuration"). Expected elements are <{}hibernate-configuration>

它可以是xml解析器的东西,它看起来像是在引擎盖下使用了stax。我们使用的是Hibernate 5.2.3。我们的hibernate.cfg.xml文件的头是正确的,我试了两个XSD

<?xml version="1.0" encoding='utf-8'?>
<hibernate-configuration
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <session-factory>

关于DTD

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

两者都有相同的错误。我一定是语法分析器出了点问题。

更新我已经修复了问题代码不可见的问题。我确信XML是有效的XML。
更新2我开始得到一些线索。它可以通过运行junit powermock test的代码引起。具体来说,阅读文件的com.sun.xml.internal.stream.XMLEventReaderImpl类由MockClassLoader加载。
更新3他们解决了它here通过驯服powermock一点b

@PowerMockIgnore({ "javax.xml.*", "org.xml.sax.*" })

现在我有不同的错误,希望我在正确的轨道上。

java.lang.LinkageError: loader constraint violation: when resolving overridden method "com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Lorg/w3c/dom/Node;)Ljava/lang/Object;" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, com/sun/xml/internal/bind/v2/runtime/unmarshaller/UnmarshallerImpl, and its superclass loader (instance of <bootloader>), have different Class objects for the type org/w3c/dom/Node used in the signature

通过向

@PowerMockIgnore({ "javax.xml.*", "org.xml.sax.*", "com.sun.xml.*", "com.sun.org.*"})
e1xvtsh3

e1xvtsh31#

你可以尝试更改并使用hibernate.cfg.xml文件的模板:

<!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 name="domain">

  <property name="hibernate.show_sql">false</property>
  <mapping package="com.example.dao.entity"/>

  <!-- etc -->

 </session-factory>
</hibernate-configuration>
xzv2uavs

xzv2uavs2#

在Hibernate 5.4.4中使用java version "14.0.1"时出现此错误。
意外元素(uri:"www.example.com",local:"hibernate-configuration")。http://www.hibernate.org/xsd/orm/cfg", local:"hibernate-configuration"). Expected elements are <{}hibernate-configuration>
一个简单的降级到java 8解决它。
正如在documentation中提到的,Hibernate最高可达5.4.4。支持Java 811

mitkmikd

mitkmikd3#

我也遇到了同样的问题,但解决方法很简单。我将正确的默认名称空间添加到hibernate.cfg.xml文件中。

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
    <session-factory>
    </session-factory>
</hibernate-configuration>

相关问题