I get the following error :
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
I get this error even though I'm developing a Spring boot project. I thought that the cfg.xml isn't needed in Spring boot and is instead replaced with the application.properties file which content is:
# Update tables
spring.jpa.hibernate.ddl-auto=update
# Oracle settings
spring.datasource.url=jdbc:oracle:thin:@asdsfdorasfc2.asdssf.ca:1521:dbmas
spring.datasource.username=userkoas
spring.datasource.password=dsgfsgfdgfdg454g5#2f#$@
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
spring.datasource.dbcp.maxTotal=1
spring.datasource.tomcat.max-active=1
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
# Logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.SQL=debug
# Views
spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp
spring.mvc.static-path-pattern=/resources/**
This error comes up when I call my controller that calls a HQL Query :
@RequestMapping(value = "/film/{id}", method = RequestMethod.GET)
public String read(@PathVariable("id") String id, Model model) {
Film film = filmService.getFilm(Long.parseLong(id));
// Get Genres
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
String query = "FROM Pays WHERE idfilm = " + id;
List genres = (List) session.createQuery(query).list();
System.out.println(genres);
session.flush();
session.close();
model.addAttribute("film", film);
return "film";
}
My SessionFactory Singleton :
public class Util {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed. " + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
HibernateUtiland let Spring Boot create theEntitymangaerfor you. You are working around Spring Boot and the auto configuration.