Hibernate Tutorial - Where to put the mapping file?

I follow this interesting hibernation guide here: http://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm

This manual, however, does not say where to put these files. I use the folder structure of the base 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 main folder contains java and resources at the same level, if this is not obvious in ASCII art. (EDIT: now it is.)

Where should the mapping file (Employee.hbm.xml) be displayed? The file is specified in the configuration file (hibernate.cfg.xml).

Thanks for this.

Hi,

+5
source share
3 answers

In the section " / src / main / resources ) you should place" hibernate.cfg.xml ", you should place all the model mapping files in the same directory that you define the POJO model classes.

According to the directory structure you provided, this should look 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 / display all your model files in the hibernate.cfg.xml file as shown below;

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

You can also check this out, grab my project folder,

enter image description here

+10
source

Usually it is in the same folder where the class being the mapping is.

But, checking this site , it can go anywhere, just localize your class in the hbm.xml file as follows:

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

You can put the mapping file in any folder, but you must specify the correct path in the hibernate.cfg.xml file for this file.

+1
source

All Articles