使用mvn install
命令将项目安装到本地存储库时,hibernate映射文件将从生成的 JAR 中排除。
我在 src / main / resources / traffic_domain / mapping / 下有 * .hbm.xml 文件,因此它应该没问题,因为它遵循标准目录布局对于maven。
我的POM文件看起来像这样:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>masters.traffic</groupId>
<artifactId>traffic_domain</artifactId>
<packaging>jar</packaging>
<name>traffic_domain</name>
<version>0.1.0</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
目录结构如下:
traffic_domain
|-- pom.xml
|-- src
|-- main
|-- java
|-- ...
|-- resources
|-- traffic_domain
|-- mapping
|-- Access.hbm.xml
|-- *.hbm.xml
traffic_domain.jar 是我的 Web应用程序运行所需的JAR之一。在启动 tomcat 时,我遇到了这样的异常:
引起: org.hibernate.MappingNotFoundException: 资源: 主/资源/ traffic_domain /映射/ Access.hbm.xml 没找到...
如何解决这个问题?
更新:确定(广告 Pascal Thivent 评论),这是调用mvn clean package
后生成的JAR的内容:
c:\Users\jwa\Desktop\tets\traffic_domain\target>jar xvf traffic_domain-0.1.0.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
created: main/
created: main/java/
created: main/java/traffic_domain/
created: main/java/traffic_domain/bean/
created: main/java/traffic_domain/logic/
created: main/java/traffic_domain/tools/
inflated: Access.hbm.xml
inflated: District.hbm.xml
inflated: main/java/traffic_domain/bean/Access.class
inflated: main/java/traffic_domain/bean/District.class
inflated: main/java/traffic_domain/bean/PostalCode.class
inflated: main/java/traffic_domain/bean/Street.class
inflated: main/java/traffic_domain/bean/TrafficCondition.class
inflated: main/java/traffic_domain/logic/AccessFacade.class
inflated: main/java/traffic_domain/logic/LocationFacade.class
inflated: main/java/traffic_domain/logic/TrafficConditionFacade.class
inflated: main/java/traffic_domain/tools/HibernateUtil.class
inflated: PostalCode.hbm.xml
inflated: Street.hbm.xml
inflated: TrafficCondition.hbm.xml
created: META-INF/maven/
created: META-INF/maven/masters.traffic/
created: META-INF/maven/masters.traffic/traffic_domain/
inflated: META-INF/maven/masters.traffic/traffic_domain/pom.xml
inflated: META-INF/maven/masters.traffic/traffic_domain/pom.properties
为了比较,以下是Eclipce创建的JAR的内容,正在工作:
C:\Users\jwa\Desktop\correct>jar xvf traffic_domain.jar
inflated: main/java/traffic_domain/bean/PostalCode.class
inflated: traffic_domain/mapping/Access.hbm.xml
created: main/resources/
created: main/resources/traffic_domain/
inflated: traffic_domain/mapping/Street.hbm.xml
inflated: main/java/traffic_domain/logic/AccessFacade.class
inflated: main/resources/traffic_domain/mapping/TrafficCondition.hbm.xml
inflated: traffic_domain/mapping/PostalCode.hbm.xml
created: main/java/traffic_domain/bean/
inflated: main/java/traffic_domain/tools/HibernateUtil.class
created: main/
inflated: main/java/traffic_domain/bean/TrafficCondition.class
inflated: mapping/Street.hbm.xml
inflated: PostalCode.hbm.xml
inflated: main/java/traffic_domain/bean/Access.class
created: traffic_domain/mapping/
inflated: District.hbm.xml
created: traffic_domain/
inflated: traffic_domain/mapping/TrafficCondition.hbm.xml
created: main/java/traffic_domain/tools/
inflated: Access.hbm.xml
inflated: traffic_domain/mapping/District.hbm.xml
created: main/java/traffic_domain/logic/
created: mapping/
created: main/resources/traffic_domain/mapping/
inflated: mapping/TrafficCondition.hbm.xml
inflated: main/resources/traffic_domain/mapping/Access.hbm.xml
inflated: mapping/Access.hbm.xml
inflated: main/java/traffic_domain/bean/Street.class
created: main/java/
inflated: main/java/traffic_domain/logic/TrafficConditionFacade.class
inflated: main/resources/traffic_domain/mapping/PostalCode.hbm.xml
created: main/java/traffic_domain/
inflated: TrafficCondition.hbm.xml
inflated: main/resources/traffic_domain/mapping/District.hbm.xml
inflated: mapping/PostalCode.hbm.xml
inflated: Street.hbm.xml
inflated: main/resources/traffic_domain/mapping/Street.hbm.xml
inflated: main/java/traffic_domain/logic/LocationFacade.class
inflated: main/java/traffic_domain/bean/District.class
inflated: mapping/District.hbm.xml
以下是 hibernate.cfg.xml 的一部分,它正在加载映射(在 axtavt 建议之后更改):
<mapping resource="traffic_domain/mapping/Access.hbm.xml"/>
<mapping resource="traffic_domain/mapping/Street.hbm.xml"/>
<mapping resource="traffic_domain/mapping/District.hbm.xml"/>
<mapping resource="traffic_domain/mapping/PostalCode.hbm.xml"/>
<mapping resource="traffic_domain/mapping/TrafficCondition.hbm.xml"/>
答案 0 :(得分:3)
通过默认,src/main/resources
中的资源应该在构建过程中复制到target/classes
。但正如我在your previous question中提到的,这里的问题是以下几行:
<sourceDirectory>src</sourceDirectory>
由于这一行,Maven正在考虑src
下的所有内容作为来源,main/resources
被复制到target/classes
。因此,在使用“默认布局”时,您仍然没有使用Maven的默认配置,而是将src/main/resources
的内容复制到target/classes
,而不包含main/resources
。< / p>
所以,正如我在my answer中所推荐的那样,使用Maven的默认值(默认布局,默认配置),特别是如果你是Maven初学者:
src/main/java
(并删除sourceDirectory
元素)我不知道Eclipse下的工作原理是什么......但是你的POM显然不正确。
后续行动:您的项目仍然存在一些问题:main/java
不应该是软件包名称的一部分,为什么资源最终会出现在软件包的根目录下罐?你能展示一下POM的最新版本吗?
关于Eclipse生产的jar,它可能正在工作,但我看到它时唯一看到的是巨大的混乱(重复文件,错误的Java包装等)。这可能是由于Maven项目设置和Eclipse项目设置之间的差异造成的。
我不知道您是否使用m2eclipse,但这是我的建议。 Eclipse下的项目设置需要与Maven项目设置保持一致,而目前却没有。 m2eclipse可以通过从POM中导出设置来为您完成。
答案 1 :(得分:1)
如果需要在阶段编译时包含* .hbm.xml;编辑你的pom.xml并添加下一个代码:
<build>
<resources>
<resource>
<directory>source/com/qfund/orm/</directory>
<targetPath>com/qfund/orm/</targetPath>
<includes>
<include>*.hbm.xml</include>
</includes>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/java/</directory>
<includes>
<include>*.xml</include>
<include>*.xsd</include>
<include>*.xslt</include>
<include>*.properties</include>
</includes>
</testResource>
</testResources>
</build>
答案 2 :(得分:0)
查看异常:
引起:org.hibernate.MappingNotFoundException:resource: 在...
找不到main / resources / traffic_domain / mapping / Access.hbm.xml
Hibernate尝试在main/resources/traffic_domain/mapping/Access.hbm.xml
找到一个映射文件,当它应该被traffic_domain/mapping/Access.hbm.xml
访问时。您可能在.cfg.xml
中指定了映射文件的错误路径。