17

I have the hibernate.cfg.xml file in the project root folder.

If I run an application containing:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

it crashes in the configure() method with the following message:

Could not locate cfg.xml resource [hibernate.cfg.xml]

Output:

Jun 17, 2016 12:04:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.connection.pool_size=5}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
    at o.k.j.HibernateTest.main(HibernateTest.java:22)
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

10 Answers10

49

If you are using Maven to build your project, place the 'hibernate.cfg.xml' file under 'src/main/resources'

Community
  • 1
  • 1
Aditya Gupta
  • 633
  • 1
  • 4
  • 11
16

If you are using Maven as build tool then place hibernate.cfg.xml under resources folder.

Steps to make application refer Hibernate Configuration File.

Right click the resources folder and select Mark Directory As option and click on Resources Root.

These steps worked for me in InteliJ IDEA Ultimate 14.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
15

The hibernate.cfg.xml file should be moved to src folder.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
3

Moving hibernate.cfg.xml file to src/main/resources solved my problem

User
  • 1,460
  • 14
  • 11
1

Below solutions might work for Maven, Intellij

  1. move "hibernate.cfg.xml" to resources folder -> rebuild project enter image description here

  2. right-click on resources folder -> then unmark resources root -> mark it resources root again enter image description here

    enter image description here

Ishika Jain
  • 949
  • 2
  • 11
  • 23
1

If you are using intellij IDEA. put the file in src/main/resources

0

Use forward / to put cfg file without forward / it won't work

Anil Amane
  • 555
  • 7
  • 12
0

I was also facing the similar issue, and I noticed that my hibernate configuration file "hibernate.cfg.xml" was not on the required path. Hence I moved hibernate.cfg.xml into src folder and that resolved the issue.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
0

It must be added to the configuration file as a persistence type.

Project Settings > Facets > Add Hibernate > Config Path

And you can see it configured in View > Tools Windows > Persistence

GL

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
0

hibernate.cfg.xml

if you are using eclipse then It must be added to the configuration as src\main\java\hibernate.cfg.xml

General Grievance
  • 4,555
  • 31
  • 31
  • 45