今天真是可笑啊,在spring和hibernate集成的时候,把hibernate的映射文件路径放到"sessionFactory"配置项中。结果报错,说是:
- Error creating bean with name 'sessionFactory' defined in file [E:\soft\Apache Software Foundation\Tomcat 7.0\webapps\ExamSystem\WEB-INF\classes\applicationContext-db.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [classpath:com/exam/model/*.hbm.xml] cannot be opened because it does not exist
我检查了很久,路径没写错啊,怎么会FileNotFound 呢?于是查看jar包有没有少,web.xml 配置有没有写错,折腾了很久都没发现什么问题。百度、google很久也没找到答案。
最后却发现这是一个脑残的错误!我是这么写的:
- <property name="mappingResources">
- <list>
- <value>classpath:com/exam/model/*.hbm.xml</value>
- </list>
- </property>
我完全没注意啊,我以为 mappingResources 里面可以这么写的。后来把“mappingResources ”改成了“mappingLocations”就没问题了。至于它们的区别,摘录一篇文章,地址:http://wukaxi.iteye.com/blog/1401753
内容如下:
- mappingResources、mappingLocations、mappingDirectoryLocations、mappingJarLocations
- 他们的区别:
- 1. mappingResources:指定classpath下具体映射文件名
- <property name="mappingResources">
- <value>petclinic.hbm.xml </value>
- </property>
- 2. mappingLocations:可以指定任何文件路径,并且可以指定前缀:classpath、file等
- <property name="mappingLocations">
- <value>/WEB-INF/petclinic.hbm.xml </value>
- </property>
- <property name="mappingLocations">
- <value>classpath:/com/company/domain/petclinic.hbm.xml </value>
- </property>
- 也可以用通配符指定,'*'指定一个文件(路径)名,'**'指定多个文件(路径)名,例如:
- <property name="mappingLocations">
- <value>classpath:/com/company/domainmaps/*.hbm.xml </value>
- </property>
- 上面的配置是在com/company/domain包下任何maps路径下的hbm.xml文件都被加载为映射文件
- 3. mappingDirectoryLocations:指定映射的文件路径
- <property name="mappingDirectoryLocations"> <list>
- <value>WEB-INF/HibernateMappings</value>
- </list>
- </property>
- 也可以通过classpath来指出
- <property name="mappingDirectoryLocations">
- <list>
- <value>classpath:/XXX/package/</value>
- </list>
- </property>