org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] (using SQLite, if that matters)

Could someone please help me figure out why I’m getting the following problem?:

/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -javaagent:/home/user/idea-IC-232.9559.62/lib/idea_rt.jar=45059:/home/user/idea-IC-232.9559.62/bin -Dfile.encoding=UTF-8 -classpath /home/user/IdeaProjects/HibernateLearningVideos03And04/target/classes:/home/user/.m2/repository/org/hibernate/orm/hibernate-core/6.2.7.Final/hibernate-core-6.2.7.Final.jar:/home/user/.m2/repository/jakarta/persistence/jakarta.persistence-api/3.1.0/jakarta.persistence-api-3.1.0.jar:/home/user/.m2/repository/jakarta/transaction/jakarta.transaction-api/2.0.1/jakarta.transaction-api-2.0.1.jar:/home/user/.m2/repository/org/jboss/logging/jboss-logging/3.5.0.Final/jboss-logging-3.5.0.Final.jar:/home/user/.m2/repository/org/hibernate/common/hibernate-commons-annotations/6.0.6.Final/hibernate-commons-annotations-6.0.6.Final.jar:/home/user/.m2/repository/io/smallrye/jandex/3.0.5/jandex-3.0.5.jar:/home/user/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar:/home/user/.m2/repository/net/bytebuddy/byte-buddy/1.12.18/byte-buddy-1.12.18.jar:/home/user/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/4.0.0/jakarta.xml.bind-api-4.0.0.jar:/home/user/.m2/repository/jakarta/activation/jakarta.activation-api/2.1.0/jakarta.activation-api-2.1.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/4.0.2/jaxb-runtime-4.0.2.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-core/4.0.2/jaxb-core-4.0.2.jar:/home/user/.m2/repository/org/eclipse/angus/angus-activation/2.0.0/angus-activation-2.0.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/txw2/4.0.2/txw2-4.0.2.jar:/home/user/.m2/repository/com/sun/istack/istack-commons-runtime/4.1.1/istack-commons-runtime-4.1.1.jar:/home/user/.m2/repository/jakarta/inject/jakarta.inject-api/2.0.1/jakarta.inject-api-2.0.1.jar:/home/user/.m2/repository/org/antlr/antlr4-runtime/4.10.1/antlr4-runtime-4.10.1.jar:/home/user/.m2/repository/org/xerial/sqlite-jdbc/3.42.0.0/sqlite-jdbc-3.42.0.0.jar:/home/user/.m2/repository/org/hibernate/orm/hibernate-community-dialects/6.2.7.Final/hibernate-community-dialects-6.2.7.Final.jar com.mycompany.hibernatelearningvideos03and04.client.ClientCode
Hello World!
Sep. 06, 2023 6:09:52 A.M. org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.7.Final
Sep. 06, 2023 6:09:52 A.M. org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
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:244)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:358)
	at org.hibernate.cfg.Configuration.configure(Configuration.java:343)
	at com.mycompany.hibernatelearningvideos03and04.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
	at com.mycompany.hibernatelearningvideos03and04.client.ClientCode.main(ClientCode.java:18)

Process finished with exit code 0

ClientCode.java:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */

package com.mycompany.hibernatelearningvideos03and04.client;

import com.mycompany.hibernatelearningvideos03and04.model.Student;
import com.mycompany.hibernatelearningvideos03and04.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class ClientCode {

    public static void main(String[] args) {
        System.out.println("Hello World!"); 
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            
            Student st = new Student();
            st.setId(1);
            st.setName("Hibernate");
            st.setEmail("hibernate@123");
            st.setPhone(12345);
             
            session.save(st);
        } catch(Exception e) {
            //
        }
    }
}

Student.java:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.hibernatelearningvideos03and04.model;

public class Student {
    private int id;
    private String name;
    private String email;
    private long phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getPhone() {
        return phone;
    }

    public void setPhone(long phone) {
        this.phone = phone;
    }
}

student.hbm.xml:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
   <class name = "Student" table = "students">
      
      <id name = "id" type = "int" column = "id">
         <generator class="increment"/>
      </id>
      
      <property name = "name" column = "name" type = "string"/>
      <property name = "email" column = "email"/> <!-- No need to explicitly state string type. -->
      <property name = "phone" column = "phone" type = "Long"/>
      
   </class>
</hibernate-mapping>

hibernate.cfg.xml (found in both /home/user/IdeaProjects/HibernateLearningVideos03And04/src/main/resources/hibernate.cfg.xml and /home/user/IdeaProjects/HibernateLearningVideos03And04/src/hibernate.cfg.xml because I read conflicting information online, so I figured I’d have the same file in both locations):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- Database connection settings -->
        <property name="connection.driver_class">org.sqlite.JDBC</property>
        <property name="connection.url">jdbc:sqlite:mydatabaseyo.sqlite</property>
        
        <!-- SQL dialect for SQLite -->
        <!-- <property name="dialect">org.hibernate.dialect.SQLiteDialect</property> -->
        <property name="hibernate.dialect">org.hibernate.community.dialect.SQLiteDialect</property> <!-- <property name="hibernate.dialect">org.hibernate.dialect.SQLiteDialect</property> -->
        <mapping resource="com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

HibernateUtil.java:

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.mycompany.hibernatelearningvideos03and04.util;

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
    private static SessionFactory sessionFactory;
    
    static {
        if(sessionFactory == null) {
            try {
                Configuration cfg = new Configuration().configure();
                ///home/user/NetBeansProjects/HibernateLearningVideos03And04/src/main/java/
                cfg.addResource("com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml");
                StandardServiceRegistry ssRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
                sessionFactory = cfg.buildSessionFactory(ssRegistry);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>HibernateLearningVideos03And04</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <exec.mainClass>com.mycompany.hibernatelearningvideos03and04.HibernateLearningVideos03And04</exec.mainClass>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.7.Final</version>
        </dependency>
         <!--https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc--> 
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.42.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-community-dialects -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-community-dialects</artifactId>
            <version>6.2.7.Final</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

Any input would be greatly appreciated!

P.S.
I tried this in NetBeans and then I did this again in IntelliJ Community Edition; the above is via IntelliJ Community Edition.

Not sure how your are building/running your application, but it seems like the hibernate.cfg.xml is simply not in the right place.
Usually, this should go into src/main/resources in a Maven project. Don’t forget to reimport the project into your IDE so that the IDE project model picks up the changes done to the Maven project model on the file system.
Also, remove the <build> section in the pom.xml. It’s best to separate resources and Java files.

If I remove the build part of the pom.xml file, the output’s error changes.

I keep on right-clicking on the project and then clicking Rebuild Module before I run the class with the main method (ClientCode), but it’s worth noting that when this wasn’t working in NetBeans, I loaded the project for the first time in IntelliJ IDEA Community Edition, and the exact same thing had happened (the error before the current one).

The new error (or technically the old error for me since I had this error before posting here) is to the following.:

/usr/lib/jvm/java-1.17.0-openjdk-amd64/bin/java -javaagent:/home/user/idea-IC-232.9559.62/lib/idea_rt.jar=46099:/home/user/idea-IC-232.9559.62/bin -Dfile.encoding=UTF-8 -classpath /home/user/IdeaProjects/HibernateLearningVideos03And04/target/classes:/home/user/.m2/repository/org/hibernate/orm/hibernate-core/6.2.7.Final/hibernate-core-6.2.7.Final.jar:/home/user/.m2/repository/jakarta/persistence/jakarta.persistence-api/3.1.0/jakarta.persistence-api-3.1.0.jar:/home/user/.m2/repository/jakarta/transaction/jakarta.transaction-api/2.0.1/jakarta.transaction-api-2.0.1.jar:/home/user/.m2/repository/org/jboss/logging/jboss-logging/3.5.0.Final/jboss-logging-3.5.0.Final.jar:/home/user/.m2/repository/org/hibernate/common/hibernate-commons-annotations/6.0.6.Final/hibernate-commons-annotations-6.0.6.Final.jar:/home/user/.m2/repository/io/smallrye/jandex/3.0.5/jandex-3.0.5.jar:/home/user/.m2/repository/com/fasterxml/classmate/1.5.1/classmate-1.5.1.jar:/home/user/.m2/repository/net/bytebuddy/byte-buddy/1.12.18/byte-buddy-1.12.18.jar:/home/user/.m2/repository/jakarta/xml/bind/jakarta.xml.bind-api/4.0.0/jakarta.xml.bind-api-4.0.0.jar:/home/user/.m2/repository/jakarta/activation/jakarta.activation-api/2.1.0/jakarta.activation-api-2.1.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-runtime/4.0.2/jaxb-runtime-4.0.2.jar:/home/user/.m2/repository/org/glassfish/jaxb/jaxb-core/4.0.2/jaxb-core-4.0.2.jar:/home/user/.m2/repository/org/eclipse/angus/angus-activation/2.0.0/angus-activation-2.0.0.jar:/home/user/.m2/repository/org/glassfish/jaxb/txw2/4.0.2/txw2-4.0.2.jar:/home/user/.m2/repository/com/sun/istack/istack-commons-runtime/4.1.1/istack-commons-runtime-4.1.1.jar:/home/user/.m2/repository/jakarta/inject/jakarta.inject-api/2.0.1/jakarta.inject-api-2.0.1.jar:/home/user/.m2/repository/org/antlr/antlr4-runtime/4.10.1/antlr4-runtime-4.10.1.jar:/home/user/.m2/repository/org/xerial/sqlite-jdbc/3.42.0.0/sqlite-jdbc-3.42.0.0.jar:/home/user/.m2/repository/org/hibernate/orm/hibernate-community-dialects/6.2.7.Final/hibernate-community-dialects-6.2.7.Final.jar com.mycompany.hibernatelearningvideos03and04.client.ClientCode
Hello World!
Sep. 06, 2023 9:09:30 P.M. org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.2.7.Final
Sep. 06, 2023 9:09:30 P.M. org.hibernate.cfg.Environment <clinit>
INFO: HHH000406: Using bytecode reflection optimizer
org.hibernate.boot.MappingNotFoundException: Mapping (RESOURCE) not found : com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml : origin(com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml)
	at org.hibernate.boot.jaxb.internal.XmlSources.fromResource(XmlSources.java:48)
	at org.hibernate.boot.MetadataSources.addResource(MetadataSources.java:329)
	at org.hibernate.cfg.Configuration.addResource(Configuration.java:619)
	at com.mycompany.hibernatelearningvideos03and04.util.HibernateUtil.<clinit>(HibernateUtil.java:20)
	at com.mycompany.hibernatelearningvideos03and04.client.ClientCode.main(ClientCode.java:18)

Process finished with exit code 0

File and folder locations:

user@debian:~/IdeaProjects/HibernateLearningVideos03And04$ ls -a
.  ..  .idea  mydatabaseyo.sqlite  pom.xml  src  target
user@debian:~/IdeaProjects/HibernateLearningVideos03And04$ find . -name "*"
.
./pom.xml
./src
./src/main
./src/main/java
./src/main/java/com
./src/main/java/com/mycompany
./src/main/java/com/mycompany/hibernatelearningvideos03and04
./src/main/java/com/mycompany/hibernatelearningvideos03and04/client
./src/main/java/com/mycompany/hibernatelearningvideos03and04/client/ClientCode.java
./src/main/java/com/mycompany/hibernatelearningvideos03and04/util
./src/main/java/com/mycompany/hibernatelearningvideos03and04/util/HibernateUtil.java
./src/main/java/com/mycompany/hibernatelearningvideos03and04/model
./src/main/java/com/mycompany/hibernatelearningvideos03and04/model/Student.java
./src/main/java/com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml
./src/main/resources
./src/main/resources/hibernate.cfg.xml
./src/test
./src/test/java
./src/hibernate.cfg.xml
./mydatabaseyo.sqlite
./target
./target/classes
./target/classes/com
./target/classes/com/mycompany
./target/classes/com/mycompany/hibernatelearningvideos03and04
./target/classes/com/mycompany/hibernatelearningvideos03and04/model
./target/classes/com/mycompany/hibernatelearningvideos03and04/model/student.hbm.xml
./target/classes/com/mycompany/hibernatelearningvideos03and04/model/Student.class
./target/classes/com/mycompany/hibernatelearningvideos03and04/client
./target/classes/com/mycompany/hibernatelearningvideos03and04/client/ClientCode.class
./target/classes/com/mycompany/hibernatelearningvideos03and04/util
./target/classes/com/mycompany/hibernatelearningvideos03and04/util/HibernateUtil.class
./target/generated-sources
./target/generated-sources/annotations
./target/maven-status
./target/maven-status/maven-compiler-plugin
./target/maven-status/maven-compiler-plugin/compile
./target/maven-status/maven-compiler-plugin/compile/default-compile
./target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst
./target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst
./target/maven-status/maven-compiler-plugin/testCompile
./target/maven-status/maven-compiler-plugin/testCompile/default-testCompile
./target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst
./target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst
./target/generated-test-sources
./target/generated-test-sources/test-annotations
./target/test-classes
./target/maven-archiver
./target/maven-archiver/pom.properties
./target/HibernateLearningVideos03And04-1.0-SNAPSHOT.jar
./.idea
./.idea/workspace.xml
./.idea/misc.xml
./.idea/compiler.xml
./.idea/encodings.xml
./.idea/jarRepositories.xml
./.idea/.gitignore
user@debian:~/IdeaProjects/HibernateLearningVideos03And04$

New pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>HibernateLearningVideos03And04</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <exec.mainClass>com.mycompany.hibernatelearningvideos03and04.HibernateLearningVideos03And04</exec.mainClass>
    </properties>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-core -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.2.7.Final</version>
        </dependency>
         <!--https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc--> 
        <dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.42.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.orm/hibernate-community-dialects -->
        <dependency>
            <groupId>org.hibernate.orm</groupId>
            <artifactId>hibernate-community-dialects</artifactId>
            <version>6.2.7.Final</version>
        </dependency>
    </dependencies>
<!--    <build>-->
<!--        <resources>-->
<!--            <resource>-->
<!--                <directory>src/main/java</directory>-->
<!--                <includes>-->
<!--                    <include>**/*.xml</include>-->
<!--                </includes>-->
<!--            </resource>-->
<!--        </resources>-->
<!--    </build>-->
</project>

Any other ideas? If you need me to give more information, just let me know. :slight_smile:

I don’t know Netbeans, but in IntelliJ you have to reimport a project. Right click on the project > Maven > Reload project. I suppose there is something similar in Netbeans, but either way, this is not a forum for Java beginners that need help with their IDE or project structure. This is a forum for Hibernate questions. Your issue clearly is a problem with your project setup or IDE configuration, so please consider asking this on Stackoverflow or the forums for the respective IDEs.

Also consider reading the following: