Johnny Bra
Johnny Bra

Reputation: 101

Could not locate cfg.xml resource [hibernate.cfg.xml] with Spring Boot

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;
    }
}

Upvotes: 0

Views: 769

Answers (1)

M. Deinum
M. Deinum

Reputation: 124441

I thought that the cfg.xml isn't needed in Spring boot and is instead replaced with the application.properties file which content is:

It isn't if you would actually use Spring Boot. But the fact that you are using a HibernateUtil means you are working around Spring Boot and its auto configuration. So in short ditch the HibernateUtil you are using and instead use JPA instead of plain Hibernate.

The code you wrote belongs in a repository or service instead of in the controller and instead of the SessionFactory inject the transactional EntityManager instead.

public class GenreRepository {

  @PersistenceContext
  private EntityManager em;

  @Transactional
  public List<Genre> findForMovie(Movie movie) {
    String query = "FROM Pays WHERE idfilm = :id";
    return em.createQuery(query, Genre.class).setParameter("id", movie.getId() ).getResultList();
  }
}

Which is all you need. Spring Boot will auto configure the JPA part, datasource etc. The same thing you should probably be doing for your FilmService as well.

Upvotes: 1

Related Questions