java Hibernate 教程 - 映射文件放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30947542/
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-11-02 17:52:15  来源:igfitidea点击:

Hibernate Tutorial - Where to put Mapping File?

javahibernatemaven

提问by user3870315

I am following this interesting tutorial on hibernate here: http://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm

我在这里关注有关休眠的有趣教程:http: //www.tutorialspoint.com/hibernate/hibernate_native_sql.htm

This tutorial, however, neglects to mention where to put these files. I am using a folder structure of a basic Maven project.

然而,本教程忽略了放置这些文件的位置。我正在使用基本 Maven 项目的文件夹结构。

The folder structure is as follows:

文件夹结构如下:

foo
└───src
    └───main
        ├───java
        │   └───org
        │       └───me
        │           └───bar 
        │               └───[all my java source-files here]
        └───resources
            ├───hibernate.cfg.xml
            └───hiber
                └───Employee.hbm.xml

The folder mainhas javaand resourcesat the same level if this is not obvious by the ASCII art. (EDIT: now it is.)

如果 ASCII 艺术不明显,则该文件夹main具有javaresources处于同一级别。(编辑:现在是。)

Where should the mapping file (Employee.hbm.xml) go? The file is referenced in the configuration file (hibernate.cfg.xml).

映射文件(Employee.hbm.xml)应该去哪里?该文件在配置文件 (hibernate.cfg.xml) 中被引用。

Thank-you for reading this.

谢谢您阅读此篇。

Regards,

问候,

回答by Levent Divilioglu

You should put the "hibernate.cfg.xml" under "/src/main/resources" You should put all the model-mapping files under the same directory that you define the POJO model classes.

您应该将“ hibernate.cfg.xml”放在“ /src/main/resources”下您应该将所有模型映射文件放在您定义POJO模型类的同一目录下。

According to the directory structure that you've provided, it should be like;

根据您提供的目录结构,它应该是这样的;

foo
└───src
    └───main
        ├───java
        │   └───org
        │       └───me
        │           └───bar 
        │               └───[all your java Model source-files here]
        |                    Employee.java
        |                    Employee.hbm.xml
        |                    Customer.java
        |                    Customer.hbm.xml
        └───resources
            └───hibernate.cfg.xml

And you should reference/map all your Model files in your hibernate.cfg.xmlfile like below;

您应该在hibernate.cfg.xml文件中引用/映射所有模型文件,如下所示;

    <mapping resource="org/me/bar/Employee.hbm.xml"/>
    <mapping resource="org/me/bar/Customer.hbm.xml"/>

You can also check this out, a capture of my project folder;

你也可以看看这个,我的项目文件夹的截图;

enter image description here

在此处输入图片说明

回答by Alex Sifuentes

Usually, it goes in the same folder where the class that are mapping are.

通常,它位于映射类所在的同一文件夹中。

But checking this site, aparently it can goes anywhere, just localize your class in the hbm.xmlfile like this:

但是检查这个站点,显然它可以去任何地方,只需hbm.xml像这样在文件中本地化你的类:

...
<hibernate-mapping>
    <class name="where.my.class.Is" table="myTable"
...

回答by DannyGolhar

You can put mapping file in any folder, but you have to provide correct path in hibernate.cfg.xml for that file.

您可以将映射文件放在任何文件夹中,但您必须在 hibernate.cfg.xml 中为该文件提供正确的路径。

回答by Ashok Kumar Dhulipalla

You can put hibernate.cfg.xml anywhere in classpath, but during the configuration you should provide the file path, e.g. if the file path is

您可以将 hibernate.cfg.xml放在 classpath 的任何位置,但在配置期间您应该提供文件路径,例如,如果文件路径是

src/main/java/conf/hibernate.cfg.xml

src/main/java/conf/hibernate.cfg.xml

you need to provide the following path:

您需要提供以下路径:

conf/hibernate.cfg.xml

conf/hibernate.cfg.xml

If you don't want to provide the path, then put hibernate.cfg.xmlin any source folder, as src/main/java/hibernate.cfg.xmlor src/main/resorces/hibernate.cfg.xmlfor example

如果你不想提供的路径,然后把hibernate.cfg.xml的任何源文件夹,为的src / main / JAVA / hibernate.cfg.xml的的src / main / resorces / hibernate.cfg.xml的例如

In the same way you can keep mapping fileanywhere in the classpath, but you need to provide the path in mapping.

以同样的方式,您可以在类路径中的任何位置保留映射文件,但您需要在映射中提供路径。

If you want to provide only the file name instead of the path, then keep it in any source folderor in the package of the class, which is mapped.

如果您只想提供文件名而不是路径,请将其保存在任何源文件夹或映射的类包中