hibernate - 项目根文件夹中的 ConfigurationException : Could not locate cfg. xml 资源 [hibernate.cfg.xml]

标签 hibernate hibernate.cfg.xml

我的项目根文件夹中有hibernate.cfg.xml文件。

如果我运行的应用程序包含:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

它在 configure() 方法中崩溃,并显示以下消息:

Could not locate cfg.xml resource [hibernate.cfg.xml]

输出:

Jun 17, 2016 12:04:59 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.0.Final}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000205: Loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.h2.Driver, hibernate.service.allow_crawling=false, hibernate.max_fetch_depth=5, hibernate.dialect=org.hibernate.dialect.H2Dialect, hibernate.format_sql=true, hibernate.generate_statistics=true, hibernate.connection.username=sa, hibernate.connection.url=jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=10000, hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.connection.pool_size=5}
Jun 17, 2016 12:04:59 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:258)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:244)
    at o.k.j.HibernateTest.main(HibernateTest.java:22)

最佳答案

如果您使用 Maven 构建项目,请将“hibernate.cfg.xml”文件放在“src/main/resources”下

关于hibernate - 项目根文件夹中的 ConfigurationException : Could not locate cfg. xml 资源 [hibernate.cfg.xml],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879613/

相关文章:

hibernate - 为什么 Hibernate 找不到/hibernate.cfg.xml?

java - "Exception in thread "主要 "org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml"

java - Hibernate 4 注释配置

java - 如何修复 my/hibernate.cfg.xml 文件?

java - JPA:如何创建与实体类型相同的字段?

hibernate :@UniqueConstraint与@ManyToOne字段?

java - hibernate/JPA : how to handle behaviour for join

java - 有没有办法禁用 Hibernate 乐观锁定?

java - Spring Data JPA(MySQL)将String保存为指数形式而不是数字