gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:25:22 25 4
gpt4 key购买 nike

我正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com