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:
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
Post a Comment