我得到以下错误:
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]即使我正在开发一个Spring引导项目,我也会遇到这个错误。我认为在Spring中不需要cfg.xml,取而代之的是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 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;
}
}发布于 2020-06-23 06:35:29
我认为在Spring中不需要cfg.xml,取而代之的是
application.properties文件,该文件的内容如下:
如果您真的要使用使用 Spring,情况就不是这样了。但是,您使用的是HibernateUtil,这意味着您正在研究Spring及其自动配置。因此,简而言之,您使用的是HibernateUtil,而不是普通的Hibernate,而是使用JPA。
您编写的代码属于存储库或服务,而不是控制器,而不是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将自动配置JPA部件、数据源等。您可能也应该为您的FilmService做同样的事情。
https://stackoverflow.com/questions/62528006
复制相似问题