12

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,

KiriSakow
  • 957
  • 1
  • 12
  • 22
user3808269
  • 1,321
  • 3
  • 21
  • 40

4 Answers4

22

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

KiriSakow
  • 957
  • 1
  • 12
  • 22
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
  • Thank-you for the detailed reply. I was able to solve my own problem shortly after posting here. I think what solved my problem was putting referencing the `` attribute correctly and coding the `hibernate-mapping` part of the of the `hbm.xml` file correctly to find the POJO at compile and runtime. :D I guess these files could be anywhere if the compiler and JRE are able to find them. – user3808269 Jun 22 '15 at 13:27
  • Yes, probably, and I'm happy that you solve your problem. By convention, I always put the hibernate configuration file ("hibernate.cfg.xml") under src/main/resources and put all the rest under the model packages that where POJO's are located. – Levent Divilioglu Jun 22 '15 at 16:07
  • how to do that with hibernate bundle? Is there any specific field where I can set hibernate-mappings? – Khatri Jul 30 '18 at 14:41
2

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"
...
Alex S. Diaz
  • 2,637
  • 2
  • 23
  • 35
  • Awesome. I think this is also correct. If I could select more than one correct answer I would also select this one. :D – user3808269 Jun 22 '15 at 13:30
2

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

DannyGolhar
  • 196
  • 1
  • 10
  • Yes, this is correct from my understanding. If I could select more than one correct answer I would also select this one. – user3808269 Jun 22 '15 at 13:29
1

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

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

you need to provide the following path:

conf/hibernate.cfg.xml

If you don't want to provide the path, then put hibernate.cfg.xml in any source folder, as src/main/java/hibernate.cfg.xml or src/main/resorces/hibernate.cfg.xml for example

In the same way you can keep mapping file anywhere 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 folder or in the package of the class, which is mapped.

scopchanov
  • 7,966
  • 10
  • 40
  • 68