仅当从 JAR 运行时才能找到 Hibernate 映射
我的项目在我的 src/main/resources 中有两个映射文件。
Foo.hbm.xml、Bar.hbm.xml
当从 eclipse 运行我的主类时,一切正常。使用maven 程序集插件导出到 jar 文件时,由于缺少映射文件,我无法创建会话工厂。错误:
Exception in thread "main" org.hibernate.MappingException: Resource: Foo.hbm.xml not found
我没有使用 spring,尽管我也尝试过使用 Spring,但遇到同样的问题 Spring 的会话工厂在从 jar 运行时找不到映射。我的实现如下。代码:
Configuration hibernateConfig = new Configuration();
hibernateConfig.configure("hibernate.cfg.xml");
hibernateConfig.addResource("Foo.hbm.xml");
hibernateConfig.addResource("Bar.hbm.xml");
hibernateConfig.setProperty("hibernate.connection.url", conf.dbUrl);
hibernateConfig.setProperty("hibernate.connection.username", conf.dbUsername);
hibernateConfig.setProperty("hibernate.connection.password", conf.dbPassword);
我也尝试通过 hibernate.cfg.xml 添加映射,但它没有产生相同的结果,在 Eclipse 中有效,但在使用可运行 JAR 时无效。我的 Maven 装配配置如下。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.app.EntryPoint</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
我使用 mvn assembly: assembly 创建 JAR,当我打开它时,映射位于 JAR 的根 (/) 中。
编辑
看完所有内容 6 小时后捂脸。 Foo.hbm.xml 拼写错误,因此映射被称为 MOTAccounts.hbm.xml,而我使用了 MotAccounts.hbm.xml。
让我放弃这种可能性的是,一切都在 Eclipse 中工作,但在 JAR 中却不起作用。就像我说的脸掌时刻。
I have project where in my src/main/resources I have two mapping files.
Foo.hbm.xml, Bar.hbm.xml
When running my main class from eclipse, all works fine. When exporting to jar file using maven assembly plugin, I cannot create session factory due to missing mapping files. Error:
Exception in thread "main" org.hibernate.MappingException: Resource: Foo.hbm.xml not found
I am not using spring altough I also tried with Spring and I have same problem Spring's session factory cannot find mapping when running from jar. My implementation is following. Code:
Configuration hibernateConfig = new Configuration();
hibernateConfig.configure("hibernate.cfg.xml");
hibernateConfig.addResource("Foo.hbm.xml");
hibernateConfig.addResource("Bar.hbm.xml");
hibernateConfig.setProperty("hibernate.connection.url", conf.dbUrl);
hibernateConfig.setProperty("hibernate.connection.username", conf.dbUsername);
hibernateConfig.setProperty("hibernate.connection.password", conf.dbPassword);
I have also tried to add mappings trough hibernate.cfg.xml but it did not work same result, works in eclipse but not when using runnable JAR. My maven assemly config is following.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.app.EntryPoint</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
I create JAR with mvn assembly:assembly and when I open it mappings are there in root (/) of a JAR.
EDIT
Facepalm moment after looking at everything for 6 hours. Foo.hbm.xml was wrongly spelled so mapping was called MOTAccounts.hbm.xml and I used MotAccounts.hbm.xml.
What threw me off of this possibility was that everything worked in eclipse, but it didn't worked in a JAR. Like I said face palm moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论