经过大量谷歌搜索后,现在感到困惑和沮丧。
我正在将一个应用程序从 Hibernate 3 升级到 4。这在使用 dtd 3.0 时效果很好,但现在需要使用 4.0 xsd,这就是一切都以 base over 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">
使用这个我得到一长串错误的根本原因是
Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 51; cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'.
我对多个 Google 页面的阅读表明我仍然需要一个令人困惑的文档类型,我认为我只是误解,当我添加一个文档类型时,Eclipse 提示 hibernate-mapping 元素的定义。忽略该错误(以防万一 eclipse 配置问题)与运行时错误相同:
Attribute "xmlns" must be declared for element type "hibernate-mapping"
我找不到使用 hibernate 4 xsd 的教程或 hbm.xml 文件示例。
谁能把我从痛苦中解救出来?
最佳答案
试试这个:
<?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 中打包的本地 XSD 文件.jar.
不知道Hibernate是否拦截了这些请求并返回本地副本like Spring does .
关于hibernate - 将 hibernate 4 与 hbm.xml 和 xsd 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17315348/