今天在applicationContext.xml中配置sessionFactory時遇到了各種頭疼的問題,現在總結一下:
1.<property name="mappingDirectoryLocations"> 如果你的xxx.hbm.xml配置文件放在了src目錄的包下面,要使用mappingDirectoryLocations管理映射文件,最好在<value>標簽中的文件目錄前加上classpath:
如:
1 <property name="mappingDirectoryLocations"> 2 <list> 3 <value>classpath:com/sims/*/model</value> 4 </list> 5 </property>
因為今天寫項目時,沒有加classpath,用junit測試service完全沒問題,但是放到了tomcat下面就出現了xxx is not mapped這樣的異常。排查了好久……
PS:當然,mappingDirectoryLocations是支持通配符的。
2.如果在讀取配置文件時,提示Unable to get the default Bean Validation factory或者帶有validation字樣的異常,那估計是少了這個配置:
1 <property name="javax.persistence.validation.mode">none</property>
這是在hibernate.cfg.xml中的寫法
但是,當我們進行SSH整合時,則需要在applicationContext.xml中配置Hibernate,那么我們需要在hibernateProperties下進行配置。同樣的,加上上面一句配置即可。
1 <property name="hibernateProperties"> 2 <props> 3 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 4 <prop key="hibernate.show_sql">true</prop> 5 <prop key="hibernate.format_sql">true</prop> 6 <prop key="javax.persistence.validation.mode">none</prop> 7 </props> 8 </property>
