使用Hibernate持久层 --- 2 出现的问题与解决方法总结
使用Hibernate持久层?---?2?出现的问题与解决方法总结
现象1:
org.springframework.beans.factory.BeanCreationException:?Error?creating?bean?with?name?’org.springframework.aop.support.DefaultPointcutAdvisor’?defined?in?null:?Can’t?resolve?reference?to?bean?’txAdvice’?while?setting?property?’advice’;?nested?exception?is?
原因:缺少Spring开头的各种包,到Spring2.0?src中编译新的dist目录出来,拷贝全部的springxxx.jar到jpetstore/WEB-INF/lib下。OK。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 现象2:
org.springframework.beans.factory.BeanCreationException:?Error?creating?bean?with?name?’sessionFactory’?defined?in?resource?[/WEB-INF/dataAccessContext-hibernate.xml]?of?ServletContext:?Instantiation?of?bean?failed;?nested?exception?is?java.lang.NoClassDefFoundError:?javax/transaction/TransactionManager
java.lang.NoClassDefFoundError:?javax/transaction/TransactionManager
原因:缺少jta.jar,OK
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 现象3:
java.lang.NoClassDefFoundError:?org/dom4j/Attribute
缺dom4j.jar
java.lang.NoClassDefFoundError:?net/sf/ehcache/CacheException
缺ehcache.jar
java.lang.NoClassDefFoundError:?net/sf/cglib/core/KeyFactory
缺cglib-full.jar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 现象4:
org.springframework.beans.factory.BeanCreationException:?Error?creating?bean?with?name?’sessionFactory’?defined?in?resource?[/WEB-INF/dataAccessContext-hibernate.xml]?of?ServletContext:?Initialization?of?bean?failed;?nested?exception?is?net.sf.hibernate.MappingException:?could?not?instantiate?id?generator
net.sf.hibernate.MappingException:?could?not?instantiate?id?generator
原因:
hbm中id字段的定义为没有定义的sequesce,修改为inc
<id
???name="id"
???type="string"
???column="userid"
??>
???<generator?class="net.sf.hibernate.id.IncrementGenerator"/>?//?原来是sequence.
??</id>
这样就可以了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象6:
org.springframework.beans.factory.BeanCreationException:?Error?creating?bean?with?name?’/shop/addItemToCart.do’?defined?in?ServletContext?resource?[/WEB-INF/petstore-servlet.xml]:?Can’t?resolve?reference?to?bean?’petStore’?while?setting?property?’petStore’;?nested?exception?is?org.springframework.beans.factory.NoSuchBeanDefinitionException:?No?bean?named?’petStore’?is?defined
org.springframework.beans.factory.NoSuchBeanDefinitionException:?No?bean?named?’petStore’?is?defined
原因:
我使用Struts作为action,当然就不需要使用Spring?WEB?framework,因此也不需要与之配套的配置文件例如,petstore-servlet.xml,
还有remote-servlet.xml?,但是Spring仍旧加载此两个文件,没办法,只好将此两个文件挪动到别的地方,但是又出现找不到这两个
文件的IO异常,没办法,只好又拷贝回来,然后把文件内容晴空。
终于好用了。
原因就是,只要你在web.xml中定义了这个servlet,他就要去找此servlet名字对应的配置文件(servletname-servlet.xml),找不到就出错。ok,现在注释掉web.xml中名字为petstore的servlet定义和影色,删除petstore-servlet.xml文件,哈哈,这下不报告错误了。
同理,注释名字为remote的servlet,删除remote-servlet.xml后,重新启动tomcat,这下不报告任何错误了。呵呵。原来如此啊。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象:
java.lang.NullPointerException
?srx.test.testhibernate.UserDAO.getAll(UserDAO.java:9)
?srx.test.struts.action.UserAction.execute(UserAction.java:20)
原因:
Action原来
UserDAO?//?这个类OK
public?class?UserDAO?extends?HibernateDaoSupport?{
?public?List?getAll()?{
???????return?this.getHibernateTemplate().find("select?aa?from?Account?aa");
?}
}
//这个类需要修改
public?class?UserAction?extends?ActionSupport?{
??UserDAO?dao?=?new?UserDAO();?
??List?accounts?=?dao.getAll();
。。。
}
修改:
//UserDAO?dao?=?new?UserDAO();?
WebApplicationContext?wac?=?this.getWebApplicationContext();
??UserDAO?dao?=?(UserDAO)?wac.getBean("userDAO");
OK