【问题标题】:Hibernate SaxParseException休眠 SaxParseException
【发布时间】:2012-08-20 20:05:36
【问题描述】:

我在一个 Maven 项目中工作。 Atm 我只有实体、映射和配置文件。我正在尝试使用 junit 测试我的休眠映射。

我找不到问题。我可以在 netbeans 中验证 xml 文件,但是当我运行我的测试时,我得到了以下错误。

netbeans 中的验证:

XML validation started.

Checking file:/C:/Users/Err/Documents/NetBeansProjects/PinkSocks/src/main/resources/hibernate.cfg.xml...
Referenced entity at "http://localhost:8080/files/hibernate-configuration-4.0.xsd".
XML validation finished.

错误:

    Initial SessionFactory creation failed.org.hibernate.MappingException: invalid configuration
            at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:25)
            at bleh.mihihi.utils.HibernateUtil.<clinit>(HibernateUtil.java:16)
            at bleh.mihihi.utils.HibernateUtilTest.setUp(HibernateUtilTest.java:20)
    Caused by: org.hibernate.MappingException: invalid configuration
            at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:1931)
            at org.hibernate.cfg.Configuration.configure(Configuration.java:1910)
            at bleh.mihihi.utils.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
    Caused by: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 25; Document is invalid: no grammar found.
            at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198)

hibernate.cfg.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration http://localhost:8080/files/hibernate-configuration-4.0.xsd">
        <session-factory>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.url">jdbc:mysql://localhost/pinksocks</property>
            <property name="connection.username">root</property>
            <property name="connection.password">admin</property>
            <property name="connection.pool_size">1</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="current_session_context_class">thread</property>
            <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
            <property name="show_sql">true</property>
            <mapping class="bleh.mihihi.Beverage" file="Beverage.hbm.xml" />
        </session-factory>
    </hibernate-configuration>

Beverage.hbm.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <hibernate-mapping
    xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping http://localhost:8080/files/hibernate-mapping-4.0.xsd"
        package="bleh.mihihi.entities">
        <class name="Beverage" table="beverages">
            <id name="id" column="id">
                <generator class="native"/>
            </id>
            <property name="name" column="name" />
            <property name="alcohol" column="alcohol"/>
        </class>
    </hibernate-mapping>

提前致谢

错误

编辑1:

我尝试过的事情:

  • 重新输入

  • 使用 dtd

  • 将xsd放到其他地方

【问题讨论】:

  • 尝试删除hibernate.cfg.xml中的行,自己再写一遍。看看有没有效果! 之后可能是一些特殊字符或其他东西
  • 在使用 junit 进行测试时,此位置是否可用? localhost:8080/files/hibernate-configuration-4.0.xsd
  • 重新输入无效。 xsd 文件与项目分开部署。

标签: java xml hibernate netbeans xsd


【解决方案1】:

对于 Hibernate 4.x,您应该使用与 3.x 相同的 DTD:

<?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-configuration>

    <session-factory>

正如您在 bug report 中看到的那样,弃用警告已被删除。 4.2版本的教程也使用这个,你可以看到here

【讨论】:

    【解决方案2】:

    您忘记指定 DTD。

    尝试在两个 xml 文件中保留以下代码并告诉我

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

    【讨论】:

    • 我想使用 4.0 xsd 文件而不是 DTD。我之前尝试过切换,但也没有用。
    • 我也遇到过这个问题。
    • 确保你也记得从根元素中删除 xsd 信息。 “xmlns”不是休眠映射 DTD 中的有效属性名称。
    【解决方案3】:

    你忘记了 hibernate.cfg.xml 文件中的 DTD

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-03
      • 2011-12-16
      • 2021-10-21
      • 2015-02-08
      • 2019-04-18
      • 2017-02-06
      • 2017-05-18
      • 2017-09-19
      相关资源
      最近更新 更多