mappingLocations、mappingDirectoryLocations與mappingJarLocations 區別

由於spring對hibernate配置文件hibernate.cfg.xml的集成相當好, 
所以,在項目中我一直使用spring的org.springframework.orm.hibernate.LocalSessionFactoryBean來取代hibernate.cfg.xml文件的功能 
LocalSessionFactoryBean有好幾個屬性用來查找hibernate映射文件:mappingResources、mappingLocations、mappingDirectoryLocations與mappingJarLocations 
他們的區別: 
mappingResources:指定classpath下具體映射文件名 
<propertyname="mappingResources"> 
   <value>petclinic.hbm.xml</value> 
</property> 
mappingLocations:可以指定任何文件路徑,並且可以指定前綴:classpath、file等 
<propertyname="mappingLocations"> 
   <value>/WEB-INF/petclinic.hbm.xml</value> 
</property> 

<propertyname="mappingLocations"> 
   <value>classpath:/com/company/domain/petclinic.hbm.xml</value> 
</property> 
也可以用通配符指定,'*'指定一個文件(路徑)名,'**'指定多個文件(路徑)名,例如: 
<propertyname="mappingLocations"> 
   <value>classpath:/com/company/domainmaps/*.hbm.xml</value> 
</property> 
上面的配置是在com/company/domain包下任何maps路徑下的hbm.xml文件都被加載爲映射文件 

*mappingResources與mappingLocations都用來指定映射文件的全路徑,不能僅指定目錄


mappingDirectoryLocations:指定映射的文件路徑 

<propertyname="mappingDirectoryLocations">
  <list>
 <value>WEB-INF/HibernateMappings</value>
  </list>
</property>
也可以通過classpath來指出
<propertyname="mappingDirectoryLocations">
  <list>
 <value>classpath:/XXX/package/</value>
  </list>
</property>

mappingJarLocations:指定加載的映射文件在jar文件中 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章