【问题标题】:Could not locate cfg.xml resource [hibernate.cfg.xml] with Spring Boot无法使用 Spring Boot 找到 cfg.xml 资源 [hibernate.cfg.xml]
【发布时间】:2020-06-23 05:38:01
【问题描述】:

我收到以下错误:

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

即使我正在开发 Spring Boot 项目,我也会收到此错误。我认为 cfg.xml 在 Spring boot 中不需要,而是替换为 application.properties 文件,其内容为:

# 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/**

当我调用调用 HQL 查询的控制器时出现此错误:

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

我的 SessionFactory 单例:

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

【问题讨论】:

  • 请不要...放弃HibernateUtil,让Spring Boot为您创建Entitymangaer。您正在处理 Spring Boot 和自动配置。
  • @M.Deinum 快速回复大声笑,我会检查一下,谢谢

标签: java spring spring-boot hibernate


【解决方案1】:

我认为在 Spring boot 中不需要 cfg.xml,而是将其替换为 application.properties 文件,其内容为:

如果您真的使用 Spring Boot,则不会。但是您使用HibernateUtil 的事实意味着您正在处理 Spring Boot 及其自动配置。所以简而言之,放弃你正在使用的HibernateUtil,而是使用 JPA 而不是普通的 Hibernate。

您编写的代码属于存储库或服务,而不是控制器,而不是SessionFactory,而是注入事务性EntityManager

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

这就是你所需要的。 Spring Boot 将自动配置 JPA 部分、数据源等。您可能也应该为 FilmService 做同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2016-06-11
    • 2016-06-14
    • 2016-10-19
    • 1970-01-01
    • 2014-07-26
    相关资源
    最近更新 更多