abguorui0 2009-12-28
使用Hibernate持久层 --- 2 出现的问题与解决方法总结
现象1:
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname’org.springframework.aop.support.DefaultPointcutAdvisor’definedinnull:Can’tresolvereferencetobean’txAdvice’whilesettingproperty’advice’;nestedexceptionis
原因:缺少Spring开头的各种包,到Spring2.0src中编译新的dist目录出来,拷贝全部的springxxx.jar到jpetstore/WEB-INF/lib下。OK。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~现象2:
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname’sessionFactory’definedinresource[/WEB-INF/dataAccessContext-hibernate.xml]ofServletContext:Instantiationofbeanfailed;nestedexceptionisjava.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:Errorcreatingbeanwithname’sessionFactory’definedinresource[/WEB-INF/dataAccessContext-hibernate.xml]ofServletContext:Initializationofbeanfailed;nestedexceptionisnet.sf.hibernate.MappingException:couldnotinstantiateidgenerator
net.sf.hibernate.MappingException:couldnotinstantiateidgenerator
原因:
hbm中id字段的定义为没有定义的sequesce,修改为inc
<id
name="id"
type="string"
column="userid"
>
<generatorclass="net.sf.hibernate.id.IncrementGenerator"/>//原来是sequence.
</id>
这样就可以了。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
现象6:
org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname’/shop/addItemToCart.do’definedinServletContextresource[/WEB-INF/petstore-servlet.xml]:Can’tresolvereferencetobean’petStore’whilesettingproperty’petStore’;nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nobeannamed’petStore’isdefined
org.springframework.beans.factory.NoSuchBeanDefinitionException:Nobeannamed’petStore’isdefined
原因:
我使用Struts作为action,当然就不需要使用SpringWEBframework,因此也不需要与之配套的配置文件例如,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
publicclassUserDAOextendsHibernateDaoSupport{
publicListgetAll(){
returnthis.getHibernateTemplate().find("selectaafromAccountaa");
}
}
//这个类需要修改
publicclassUserActionextendsActionSupport{
UserDAOdao=newUserDAO();
Listaccounts=dao.getAll();
。。。
}
修改:
//UserDAOdao=newUserDAO();
WebApplicationContextwac=this.getWebApplicationContext();
UserDAOdao=(UserDAO)wac.getBean("userDAO");
OK