0

I got the following error while configuring hibernate.cfg.xml

Exception in thread "main" 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:165) at org.hibernate.cfg.Configuration.configure(Configuration.java:258) at StartWithHibernate.main(StartWithHibernate.java:25)

Directory structure:

enter image description here

Code of Person.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="hibernate_test.Person" table="person" catalog="hibernate_practice_database">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" not-null="true" />
        </property>
        <property name="hobby" type="string">
            <column name="hobby" length="30" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

<!--<root>
    
</root>-->

Code of StartWithHibernate.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.Serializable;
import hibernate_test.Person;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

/**
 *
 * @author Keval
 */
public class StartWithHibernate {

    public static void main(String[] args) {
        SessionFactory sessionFactory;
        ServiceRegistry serviceRegistry;
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(configuration.getProperties()).build();
// builds a session factory from the service registry
        sessionFactory
                = configuration.buildSessionFactory(serviceRegistry);
        Session openSession = sessionFactory.openSession();

        Transaction t = openSession.beginTransaction();

        System.out.println("begin transaction" + t);

        Serializable save = openSession.save(new Person(7, "aaa", "aaa"));
        openSession.saveOrUpdate(new Person(8, "bbb", "aaa"));
        openSession.persist(new Person(9, "a", "a"));
        openSession.getTransaction().commit();
        System.out.println("saved");

        StandardServiceRegistryBuilder.destroy(serviceRegistry);

    }
}

Code of hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_practice_database</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    
    <mapping resource="Person.hbm.xml"/>
    
    <!--<mapping class="mypack.Person"/>-->
   </session-factory>
</hibernate-configuration>

<!--<root>
    
</root>-->

Code of Person.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate_test;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
//import javax.persistence.Id;
import javax.persistence.Table;

/**
 *
 * @author Keval
 */

//@Entity
//@Table(name = "person", catalog = "newdb")
public class Person //implements java.io.Serializable
{

    private int id;
    private String name;
    private String hobby;

    public Person() {
    }

    public Person(int id, String name, String hobby) {
        this.id = id;
        this.name = name;
        this.hobby = hobby;
    }

//    @Id
//
//    @Column(name = "id", unique = true, nullable = false)
    public int getId() {
        return this.id;
    }

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

//    @Column(name = "name", nullable = false, length = 30)
    public String getName() {
        return this.name;
    }

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

//    @Column(name = "hobby", nullable = false, length = 30)
    public String getHobby() {
        return this.hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", name=" + name + ", hobby=" + hobby + '}';
    }

}

Please help me to resolve this error.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Can you give this link a try - https://stackoverflow.com/questions/35725306/org-hibernate-internal-util-config-configurationexception-could-not-locate-cfg/35725560 – rifaqat Mar 09 '21 at 12:59
  • You IDE (you don't say, which one are you using) probably does not package `hibernate.cfg.xml` together with the compiled classes. – Piotr P. Karwasz Mar 09 '21 at 17:00

0 Answers0