【问题标题】:Using hibernate 4 with hbm.xml and xsd将休眠 4 与 hbm.xml 和 xsd 一起使用
【发布时间】:2013-06-23 07:31:23
【问题描述】:

经过多次谷歌搜索,现在感到困惑和沮丧。

我正在将应用程序从 Hibernate 3 升级到 4。这在使用 dtd 3.0 时效果很好,但现在需要使用 4.0 xsd,这是一切都超越 apex 的地方!

应用程序使用 hbm.xml 文件来配置每个实体,任何地方都没有注释。

找到一个 hbm 文件的示例会非常有用,但即使是 hibernate 4 的教程也只使用 3.0 dtd!

我正在使用以下

<hibernate-mapping
    xmlns="http://www.hibernate.org/xsd/hibernate-mapping"
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-mapping hibernate-mapping-4.0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    package="org.hibernate.metamodel.binding">

使用这个我得到一个很长的错误列表,根本原因是

原因:org.xml.sax.SAXParseException;行号:6;列号:51; cvc-elt.1:找不到元素“休眠映射”的声明。

我对多个 Google 页面的阅读表明我仍然需要一个令人困惑的 doctype,我认为我只是误解了,当我添加 doctype 时,Eclipse 抱怨 hibernate-mapping 元素的定义。忽略该错误(仅以防 Eclipse 配置问题)与运行时错误相同:

必须为元素类型“hibernate-mapping”声明属性“xmlns”

我一直找不到使用 hibernate 4 xsd 的 hbm.xml 文件的教程或示例。

谁能帮我摆脱痛苦?

【问题讨论】:

    标签: hibernate hbm


    【解决方案1】:

    关于“xmlns”属性的消息有点令人困惑,尽管您所要做的就是从hibernate-mapping 元素中删除所有属性,只留下package 属性。至少,它对我有用。

    【讨论】:

    • 缺少某些内容或我不明白。
    【解决方案2】:

    试试这个:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <hibernate-mapping package="your.package.name"
        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://www.hibernate.org/xsd/hibernate-mapping/hibernate-mapping-4.0.xsd">
    
        <!-- your mappings --->
    
    </hibernate-mapping>
    

    注意这指向一个外部地址 (http://www.hibernate.org)。

    理想情况下,我们会使用 Hibernate 在 hibernate-core.jar 中打包的本地 XSD 文件。

    不知道Hibernate是否会拦截这些请求并返回本地副本like Spring does

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 2013-04-03
      • 2014-12-03
      • 2019-02-09
      相关资源
      最近更新 更多