简体   繁体   English

将hibernate 4与hbm.xml和xsd一起使用

[英]Using hibernate 4 with hbm.xml and xsd

after much googling now confused and frustrated. 经过大量的搜索之后,现在感到困惑和沮丧。

I am upgrading an application from Hibernate 3 to 4. This works well using dtd 3.0 but now need to use the 4.0 xsd and this is where every thing goes base over apex! 我正在将应用程序从Hibernate 3升级到4。这可以在dtd 3.0上很好地工作,但是现在需要使用4.0 xsd,这是一切都基于顶点的地方!

The app uses hbm.xml files to configure each entity, no annotations anywhere. 该应用程序使用hbm.xml文件配置每个实体,任何地方都没有注释。

It would be very useful to find an example of an hbm file but even tutorials for hibernate 4 just use the 3.0 dtd! 查找一个hbm文件的示例将非常有用,但是即使是休眠4教程也仅使用3.0 dtd!

I am using the following 我正在使用以下

<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">

Using this I get an LONG list of errors the root cause being 使用此方法,我得到了很多错误,根本原因是

Caused by: org.xml.sax.SAXParseException; 由以下原因引起:org.xml.sax.SAXParseException; lineNumber: 6; lineNumber:6; columnNumber: 51; columnNumber:51; cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'. cvc-elt.1:找不到元素“ hibernate-mapping”的声明。

My reading of multiple Google pages suggest that I still need a doctype which is confusing and I think I am just misunderstanding and when I do add a doctype Eclipse complains about the definition of the hibernate-mapping element. 我对多个Google页面的阅读表明,我仍然需要一个令人困惑的doctype,我认为我只是误解了,当我添加一个doctype时,Eclipse抱怨了hibernate-mapping元素的定义。 Ignoring that error (just incase problem with eclipse config) get the same as a run time error: 忽略该错误(以防万一eclipse配置出现问题)与运行时错误相同:

Attribute "xmlns" must be declared for element type "hibernate-mapping" 必须为元素类型“休眠映射”声明属性“ xmlns”

I have been unable to find a tutorial or an example of an hbm.xml file that uses the hibernate 4 xsd. 我一直找不到使用休眠4 xsd的教程或hbm.xml文件的示例。

Can anyone please put me out of my misery ? 谁能让我摆脱苦难?

Try this: 尝试这个:

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

Note this points to an external address ( http://www.hibernate.org ). 请注意,这指向一个外部地址( http://www.hibernate.org )。

Ideally we would use the local XSD files that Hibernate packages in hibernate-core .jar. 理想情况下,我们将使用Hibernate打包在hibernate-core .jar中的本地XSD文件。

I don't know whether Hibernate intercepts these requests and returns the local copy like Spring does . 我不知道Hibernate是否会像Spring那样拦截这些请求并返回本地副本。

Message about "xmlns" attrribute is a bit confusing, although all you have to do is to remove all the attributes from the hibernate-mapping element and leave only package attribute. 关于“ xmlns”属性的消息有些混乱,尽管您要做的只是从hibernate-mapping元素中删除所有属性,而只保留package属性。 At least, it worked for me. 至少,它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM