Hibernate 5.1.0 错误无法执行解组并且找不到元素“休眠配置”的声明

Posted

技术标签:

【中文标题】Hibernate 5.1.0 错误无法执行解组并且找不到元素“休眠配置”的声明【英文标题】:Hibernate 5.1.0 Errors Unable to perform unmarshalling and Cannot find the declaration of element 'hibernate-configuration' 【发布时间】:2016-09-20 17:49:04 【问题描述】:

尝试创建一个包含单个条目的简单学生数据库。 我使用 Hibernate 5.1.0 和 mysql 作为具有简单学生表的数据库,并且无法连接到数据库。使用 Eclipse Mars 以及添加到项目中的 MYSQL jdbc 驱动程序以及 hibernate 5.1 必要的 jars。

我的 StudentInfo.java 文件

package com.nitish.hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="student")
public class StudentInfo 
    @Id
    private int rollno;
    private String name;
    private int marks;

    public int getRollno() 
        return rollno;
    
    public void setRollno(int rollno) 
        this.rollno = rollno;
    
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public int getMarks() 
        return marks;
    
    public void setMarks(int marks) 
        this.marks = marks;
    



我的 Main.java 文件

package com.nitish.hibernate;

import org.hibernate.*; 

public class Main 

    public static void main(String args[])

        StudentInfo s1 = new StudentInfo();

        s1.setName("Nitishpisal");
        s1.setRollno(5);
        s1.setMarks(33);


        try
            Configuration conf = new Configuration();
            System.out.println("Hibernate Configuration loaded"); 
            conf.configure("hibernate.cfg.xml");

            ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
            System.out.println("registry loaded"); 

            SessionFactory sf = conf.buildSessionFactory(sr);
            System.out.println(" session factory loaded");

            Session session = sf.openSession();

            session.beginTransaction();
            session.save(s1);
            session.getTransaction().commit();
            catch(HibernateException e)
                e.printStackTrace();
               

    

我的 hibernate.cfg.xml 文件

<!DOCTYPE hibernate-configuration PUBLIC 
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration xmlns="http://www.hibernate.org/xsd/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/test</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>

        <!-- model classes  and mapping info-->
        <mapping class="com.nitish.hibernate.StudentInfo"></property>

    </session-factory>

</hibernate-configuration>

我遇到的错误

May 23, 2016 2:16:51 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
May 23, 2016 2:16:51 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Hibernate Configuration loaded
May 23, 2016 2:16:51 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead.  Support for obsolete DTD/XSD namespaces may be removed at any time.
org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 5 and column 87 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133)
    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.nitish.hibernate.Main.main(Main.java:27)
Caused by: javax.xml.bind.UnmarshalException
 - with linked exception:
[org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 87; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.]
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:468)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:448)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:420)
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:126)
    ... 5 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 87; cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:134)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:437)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:368)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:325)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1906)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.startElement(XMLSchemaValidator.java:746)
    at com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorHandlerImpl.startElement(ValidatorHandlerImpl.java:570)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.startElement(ValidatingUnmarshaller.java:86)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor.startElement(InterningXmlVisitor.java:60)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.handleStartElement(StAXEventConnector.java:246)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StAXEventConnector.bridge(StAXEventConnector.java:115)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:445)
    ... 7 more

我们将不胜感激任何帮助!

【问题讨论】:

当我用这个 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 替换我的 Main.java 文件时工作会话会话 = sessionFactory.openSession(); session.beginTransaction(); session.save(s1); session.getTransaction().commit();谢谢 【参考方案1】:

尝试删除此字符串: xmlns="http://www.hibernate.org/xsd/hibernate-configuration"

【讨论】:

似乎它不喜欢架构:Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. 尝试将"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" 替换为此http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd Надеюсь поможет:)【参考方案2】:

在 hibernate.cfg.xml 中声明映射类后删除关闭属性标记 替换

<!-- model classes  and mapping info-->
<mapping class="com.nitish.hibernate.StudentInfo"></property>

<!-- model classes  and mapping info-->
 <mapping class="com.nitish.hibernate.StudentInfo">

【讨论】:

我做了,但同样的问题仍然存在 这没有帮助

以上是关于Hibernate 5.1.0 错误无法执行解组并且找不到元素“休眠配置”的声明的主要内容,如果未能解决你的问题,请参考以下文章

Android 和 AWS Cognito - 无法使用 proguard 解组错误响应

json:无法将字符串解组为 MyMap.map 类型的 Go 值

MarshallingException:无法解组结果参考

Grails 无法将 JSON 中的日期/时间解组回 joda DateTime

如何将 SOAP XML 解组为 Java 对象 [重复]

如何调试 JAXB 解组?