【发布时间】: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