Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Hibernate cannot find hibernate.cfg.xml at startup

Errororg.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.

  • Applies to Hibernate ORM 6.6, Maven 3.9, Java 21
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1find prints nothing, although the file exists in the source treeThe file is outside the resources folderFix
    2. 2The resource name in the message looks like a disk path or has the wrong folderconfigure() is given a file path instead of a resource nameFix

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 package

3Verify

The file now appears in the build output.

find target/classes -name "hibernate.cfg.xml"

Confirmed by maven.apache.org

Did this fix it?

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

Did this fix it?

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

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.