Hibernate cannot find hibernate.cfg.xml at startup
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]Short answer
Hibernate loads cfg.xml as a resource, not as a file on disk, so a path like /MyProject/src/hibernate.cfg.xml is not a valid location. The file must be on the runtime classpath, which for Maven means src/main/resources, and the name passed to configure() must match its path there.
Fix it fast
1Find out which cause you have
Check whether the file was copied to the build output. If it is missing there, the runtime classpath cannot see it.
find target/classes -name "hibernate.cfg.xml"2Match what you see, then jump to its fix
What does the error look like?
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
Cause 1 of 2
The file is outside the resources folder
1Check
Maven copies src/main/resources to the classpath; a file in src/ or the project root is not copied.
find . -name "hibernate.cfg.xml" -not -path "./target/*"2Fix
Move it into src/main/resources and rebuild, so it ends up at the root of the classpath.
mkdir -p src/main/resources
mv src/hibernate.cfg.xml src/main/resources/
./mvnw clean package3Verify
The file now appears in the build output.
find target/classes -name "hibernate.cfg.xml"Confirmed by maven.apache.org
Cause 2 of 2
configure() is given a file path instead of a resource name
1Check
configure() with no argument reads the default hibernate.cfg.xml resource. configure(String) takes the name of a resource, like org/hibernate/example/MyCfg.xml, which is a path inside the classpath.
grep -rn "configure(" src/main/java/2Fix
Use the no-argument form for the default name, or give the classpath-relative name of your file.
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // hibernate.cfg.xml at the classpath root
// .configure("config/my-hibernate.cfg.xml") for a custom resource
.build();3Verify
The SessionFactory builds without the exception.
SessionFactory sf = new MetadataSources(registry).buildMetadata().buildSessionFactory();Confirmed by docs.hibernate.org
How do I stop it coming back?
- Keep Hibernate config in src/main/resources and run from the build tool, not from an IDE-only path.
- Do not hard-code absolute or workspace paths in code that must run on other machines.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Hibernate cannot initialize a lazy collection or proxy in Spring Boot
could not initialize proxy - no Session
A lazy collection or proxy is loaded on first access, and that needs an open Hibernate session.
3 causes, each with a fixDatabases
MySQL Connector/J: EOFException, expected to read 4 bytes, read 0 bytes
Communications link failure
The MySQL server or something between you and it closed the connection, and the driver found out only when it tried to use it.
3 causes, each with a fixDatabases
Java cannot load org.postgresql.Driver
Class not found loading JDBC org.postgresql.Driver
The PostgreSQL JDBC jar is not on the classpath of the process that runs your code.
3 causes, each with a fix