java - 我可以在 hibernate 中使用相对路径指示映射文件吗?

标签 java hibernate orm mapping

我正在使用 hibernate 开发一个独立的应用程序,我的项目使用以下结构: enter image description here

这就是我告诉 hibernate(是的,我正在与 Hibernate 聊天)在哪里可以找到 hibernate.cfg.xml 文件的方式:

File hibernateConfigurationFile = new File("..\\resources\\hibernate.cfg.xml");
...
Configuration().configure(hibernateConfigurationFile).buildSessionFactory();

这就是我告诉 hibernate 在哪里可以找到 Event.hbm.xml 文件的方式:

<mapping resource="../resources/test/Event.hbm.xml"/>

我也尝试过:

<mapping resource="test/Event.hbm.xml"/>

但是当我编译它时,出现以下错误:

Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: test/Event.hbm.xml not found

我明白这是因为我在 hibernate.cfg.xml 文件中指示了错误的映射文件路径。所以我移动了我的 Event.hbm.xml 文件,我的项目的结构现在是: enter image description here

一切正常。

我的问题是我想将 Event.hbm.xml 保留在 resources\test 目录中。如何在我的 hibernate.cfg.xml 文件中指出这一点?我尝试了相对路径,但它不起作用。我阅读了 hibernate 文档,但什么也没找到。有什么建议吗?

最佳答案

您可以尝试programmatic way of building hibernate configuration并可以说

SessionFactory sf = new Configuration()
    .addFile("yourpath/Event.hbm.xml")

而不是在 hibernate.cfg.xml 中声明映射资源

参见this method

关于java - 我可以在 hibernate 中使用相对路径指示映射文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16012917/

相关文章:

java - 服务层中的验证(Spring Boot)

django - 在反向关系上访问 prefetch_related 的字段

java - H2 中 Hibernate ORM 的问题

Java等级计算器。我需要一些帮助来开始这个类

java - 使用sql获取平均评分

mysql - 将查询转换为 hibernate 条件查询

python - SQLAlchemy:过滤具有深层关系的模型对象?

java - 如何用Java打印Dijkstra算法的完整路径

java - TextToSpeech SynthesizeToFile 何时完成?

java - Struts 2 项目是在 My Eclipse 中构建的,但未部署在 Tomcat 服务器上