SSH常见异常拾遗
SSH常见错误拾遗? 转自:http://sslaowan.iteye.com/blog/119731最近项目在用SSH,中间常会遇到一些错误,记
SSH常见错误拾遗
? 转自:http://sslaowan.iteye.com/blog/119731
最近项目在用SSH,中间常会遇到一些错误,记录一下,以备查找。
1、找不到hibernate.cfg.xml
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/classes/hibernate.cfg.xml] cannot be resolved to URL because it does not exist
java.io.FileNotFoundException: class path resource [WEB-INF/classes/hibernate.cfg.xml] cannot be resolved to URL because it does not exist
???????????这个问题是由于在applicationContext.xml中我们指定了hibernate.cfg.xml的位置,可是在运行JUnit进行单元测试和使用Tomcat运行时,这个路径是不一样的。因此就会报找不到这个文件的错误。
????
xml 代码
- <bean?id="sessionFactory"?? ????????class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">??
- ????<property?name="configLocation">?? ???????????????<value>/WEB-INF/classes/hibernate.cfg.xml?</value>??? ??
- ?????????????????<!--??<value>/hibernate.cfg.xml</value>????-->? ?? ????</property>??
- </bean>??
???????? 我现在的做法是:在Tomcat下进行测试时,使用上面没有注释掉的那个路径,当使用JUnit测试时,就使用下面注释掉的那个路径。
?
2、org.hibernate.MappingException: Unknown entity
org.springframework.orm.hibernate3.HibernateSystemException: Unknown entity: DetailOfPlanForDayAndNight; nested exception is org.hibernate.MappingException: Unknown entity: DetailOfPlanForDayAndNight
org.hibernate.MappingException: Unknown entity: DetailOfPlanForDayAndNight
?? 从这个错误中,我才发现原来在下面这个方法中使用的那个字符串,是和配置文件对应的,具体如下:
java 代码
- public?DetailOfPlanForDayAndNight?findById(DetailOfPlanForDayAndNightId?id)?{ ?? ????log.debug("getting?DetailOfPlanForDayAndNight?instance?with?id:?"?+?id); ??
- ????try?{ ?? ????????DetailOfPlanForDayAndNight?instance?=?(DetailOfPlanForDayAndNight)?getHibernateTemplate() ??
????????????????.(1)get("DetailOfPlanForDayAndNight",?id); ??
????????return?instance; ?? - ????}?catch?(RuntimeException?re)?{ ?? ????????log.error("get?failed",?re); ??
- ????????throw?re; ?? ????} ??
- }??
??? (1)中get("DetailOfPlanForDayAndNight",id)中的那个DetailOfPlanForDayAndNight是和下面的××.hbm.xml中的配置信息是对应的,我们的配置文件是:
???
xml 代码
- <class?name="edu.bjtu.port.domain.DetailOfPlanForDayAndNight"?table="PD1_DNPLAN_DTL"?schema="SOMIS">?? ???????<composite-id?name="id"?class="edu.bjtu.port.domain.DetailOfPlanForDayAndNightId">??
- ???????????<key-property?name="planId"?type="java.lang.String">?? ???????????????<column?name="DNPLANID"?length="8"?/>??
- ???????????</key-property>?? ???????????<key-property?name="detailId"?type="java.lang.String">??
- ???????????????<column?name="NO"?length="2"?/>?? ???????????</key-property>??
- ???????</composite-id>??
????? class的name是edu.bjtu.port.domain.DetailOfPlanForDayAndNight,因此get中的那个字符串应该是edu.bjtu.port.domain.DetailOfPlanForDayAndNight,如下:
java 代码
- DetailOfPlanForDayAndNight?instance?=?(DetailOfPlanForDayAndNight)?getHibernateTemplate() ?? ???????????????????.get("edu.bjtu.port.domain.DetailOfPlanForDayAndNight",?id);??
???? 这样就OK了。
我的异常网推荐解决方案:org.springframework.beans.factory.BeanCreationException:,http://www.myexception.cn/j2ee/10759.html