java - org.hibernate.HibernateException: /hibernate.cfg.xml not found from resources -


i have new spring-boot project. use gradle build manager , eclpice ide. want use hibernate , try configurations hibernate.cfg.xml file,

org.hibernate.hibernateexception: /hibernate.cfg.xml not found

i store file in resources folder, tried put meta-inf folder.

folders structure:

enter image description here

hibernateutil.java:

public class hibernateutil {  private static  sessionfactory sessionfactory = buildsessionfactory();    private static sessionfactory buildsessionfactory()    {       try       {          if (sessionfactory == null)          {              sessionfactory = new configuration().configure().buildsessionfactory();          }          return sessionfactory;       } catch (throwable ex)       {          system.err.println("initial sessionfactory creation failed." + ex);          throw new exceptionininitializererror(ex);       }  }  public static sessionfactory getsessionfactory() {     return sessionfactory; }  public static void shutdown() {     // close caches , connection pools     getsessionfactory().close(); } 

}

if want read files in resources directory, need use classloder file.

in static method

hibernateutil.class.getclassloader().getresource("hibernate.cfg.xml").getfile(); 

non static method

getclass().getclassloader().getresource("hibernate.cfg.xml").getfile(); 

and if resource resides in of sub directories of resources, prefix folder path while getting resource

example resource in meta-inf directory

getresource("meta-inf/hibernate.cfg.xml"); 

but spring boot auto configures, adding dependencies in pom.xml , providing jpa related properties in application.properties


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -