Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Tutorial - Where to put Mapping File?

I am following this interesting tutorial on hibernate here: 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.

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 main has java and resources at the same level if this is not obvious by the ASCII art. (EDIT: now it is.)

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

Thank-you for reading this.

Regards,

like image 474
user3808269 Avatar asked Jun 19 '15 21:06

user3808269


2 Answers

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.

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.xml file like below;

    <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

like image 66
Levent Divilioglu Avatar answered Sep 30 '22 14:09

Levent Divilioglu


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.xml file like this:

...
<hibernate-mapping>
    <class name="where.my.class.Is" table="myTable"
...
like image 42
Alex Sifuentes Avatar answered Sep 30 '22 15:09

Alex Sifuentes