Skip to content
Advertisement

config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]

I am building a hibernate application that creates, modify, and query tables from a database. The project was running perfectly fine without having this error, when I try to run it now I got this error out of nowhere. The ‘hibernate.cfg.xml’ is placed in the ‘src’ folder as can be seen below. I am running it on Netbeans Apache IDE 12.2 (Not sure if that’s relevant).

project src

MainApp.java

package hiberApp;

import util.HiberUtil;
import org.hibernate.SessionFactory;

/* 
 @author Osama
 */
public final class MainApp {
    
    private static final SessionFactory SESSION_FACTORY = HiberUtil.getSessionFactory(HiberUtil.Mapping.ANN);

    public static void main(String[] args) {
        
//        final DataLoad dataLoad = new DataLoad();
//        dataLoad.createData(SESSION_FACTORY);
//        final DataQueries dataQueries = new DataQueries();
//        dataQueries.showAll(SESSION_FACTORY);
       
    }
}

HiberUtil.java

package util;

import model.*;
import java.io.File;
import static java.lang.Math.log;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Osama
 */

public final class HiberUtil {
    
//     static Logger log = LoggerFactory.getLogger(HiberUtil.class); 
    /**
     * choose type of mapping definition XML or annotations
     */
    public enum Mapping {
       /**
        * use xml defined mappings for configuration
        */
       XML, 
       /**
        * use annotation defined mappings for confuguration
        */
       ANN;
    } 
    /**
     * 
     * @param mapping
     * @return SessionFactory 
     */
    public static SessionFactory getSessionFactory(Mapping mapping) {
   
        switch(mapping){
            case XML:
                return(getXMLSessionFactory());
            case ANN:
                return(getANNSessionFactory());
            default:
               return(getXMLSessionFactory());
        }  
    }
  
    public static SessionFactory getXMLSessionFactory() {
        try {
            File mappingDir = new File("src\xmlMaps");
            Configuration config = new Configuration().configure();

            config.setProperty("hibernate.show_sql", "false");
            config.addDirectory(mappingDir);
            SessionFactory sf = config.buildSessionFactory();
                   
//            log.trace("Session Factory created "+java.time.LocalDate.now());
           
            return (sf);
        }
        catch (Throwable ex) {
          
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

 public static SessionFactory getANNSessionFactory() {
        try {
            Configuration config = new Configuration().configure();
            config.setProperty("hibernate.show_sql", "false");
                      
            config.addAnnotatedClass(Coach.class)
            .addAnnotatedClass(Matches.class)
            .addAnnotatedClass(Person.class)
            .addAnnotatedClass(Player.class)
            .addAnnotatedClass(Position.class)
            .addAnnotatedClass(Stadium.class)
            .addAnnotatedClass(Team.class);
            SessionFactory sf = config.buildSessionFactory();
           
            return (sf);
        }
        catch (Throwable ex) {
          
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

}

Error:

Caused by: 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:234)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
    at util.HiberUtil.getANNSessionFactory(HiberUtil.java:77)
    ... 2 more

Thanks In-advance :)!

Advertisement

Answer

Clean installation of NetBeans fixed the issue!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement