java Hibernate的映射问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5574285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:43:52  来源:igfitidea点击:

Mapping problem with Hibernate

javahibernateconfigurationhibernate-mapping

提问by special0ne

I am new to hibernate and i am having trouble with specifying the location of the mapping file in hibernate.cfg.xml file.

我是 hibernate 的新手,在 hibernate.cfg.xml 文件中指定映射文件的位置时遇到问题。

I have created an Event object in org.hibernate.tutorial.chapter1.domain.Event.java package with its corresponding Event.hbm.xml file in the same location.

我在 org.hibernate.tutorial.chapter1.domain.Event.java 包中创建了一个 Event 对象,其对应的 Event.hbm.xml 文件位于同一位置。

I am trying to specify the location in the hibernate.cfg.xml mapping tag but I am getting an InvalidMappingException ().

我试图在 hibernate.cfg.xml 映射标记中指定位置,但我收到了 InvalidMappingException ()。

I have added to the post: the exception, the mapping from the mapping file and the project file structure.

我在帖子中添加了:异常、映射文件和项目文件结构的映射。

any advice would be great.

任何建议都会很棒。

484 [main] ERROR org.hibernate.util.xml.ErrorLogger - Error parsing XML (1) : cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'. 495 [main] ERROR org.hibernate.util.xml.ErrorLogger - Error parsing XML (2) : cvc-elt.1: Cannot find the declaration of element 'hibernate-mapping'. Initial SessionFactory creation failed.org.hibernate.InvalidMappingException: Unable to read XML

484 [main] 错误 org.hibernate.util.xml.ErrorLogger - 解析 XML (1) 时出错:cvc-elt.1:找不到元素“hibernate-mapping”的声明。495 [main] 错误 org.hibernate.util.xml.ErrorLogger - 解析 XML 时出错 (2):cvc-elt.1:找不到元素“hibernate-mapping”的声明。初始 SessionFactory 创建失败.org.hibernate.InvalidMappingException:无法读取 XML

<!-- Names the annotated entity class -->
<mapping resource="org/hibernate/tutorial/chapter1/domain/Event.hbm.xml"/>

see below the project structure

见下面的项目结构

回答by matt b

Make sure you have a DOCTYPE in your Event.hbm.xml at the top of the XML content such as:

确保在 XML 内容顶部的 Event.hbm.xml 中有一个 DOCTYPE,例如

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="org.hibernate.tutorial.domain">
[...]
</hibernate-mapping>

回答by user3050538

My problem was that my XML file was missing :

我的问题是我的 XML 文件丢失了:

<?xml version="1.0" encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

after inserting this to the beginning of the XML file everything turned out great. Thanks!

将此插入到 XML 文件的开头后,一切都变得很好。谢谢!