spring+hibernate出错小结:
(1)java.lang.NoClassDefFoundError: org/hibernate/context/CurrentSessionContext
原因:出现这错误时,请更改hibernate的包,更新至最新或3.1以上
(2)java.lang.NoClassDefFoundError: javax/transaction/TransactionManager
原因:缺少jta.jar 或者是找不到hbm.xml文件导致sessionfactory出错,检查hbm文件路径是否正确,文件是否存在
(3) 错误:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
或者org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not insert: [com.yourcompany.model.Login]; bad SQL grammar [insert into mysql__login (name, password) values (?, ?)]; nested exception is java.sql.SQLException: Table 'mysql.mysql__login' doesn't exist java.sql.SQLException: Table 'mysql.mysql__login' doesn't exist
原因与解决:因为Hibernate Tools(或者Eclipse本身的Database Explorer)生成*.hbn.xml工具中包含有catalog="***"(*表示数据库名称)这样的属性,将该属性删除就可以了
(4)org.springframework.orm.hibernate3.HibernateQueryException: undefined alias
原因:在spring配置文件中,可能你设置了<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>,指定了HQL的解释器,请删除或更改另一个解释器org.hibernate.hql.ast.ASTQueryTranslatorFactory, 如果没有设置,请确认是否有写错了HQL语句,是否与POJO里的属性一样。注意区分大小写。
(5)quartz报错: Caused by: java.lang.NoSuchMethodError:
org.apache.commons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Set;
原因是由于使用的commons-collections.jar版本太低导致的要使用3.*的才行,下载地址为:http://commons.apache.org/downloads/download_collections.cgi
选Binary下的zip下载,下载后将里的的commons-collections-x.x.x.jar放置到你的classpath里即可。记得删除旧的那个jar包
(6)org.springframework.beans.factory.CannotLoadBeanClassException:
Cannot find class [com.mchange.v2.c3p0.ComboPooledDataSource] for bean
这是因为你的sping配置文件是使用c3p0作数据池,而你的classpath里没有找到这个c3p0的jar包而导致的,下载一个jar导入classpath即可。
地址为:http://sourceforge.net/project/showfiles.php?group_id=25357&package_id=88527,如果是windows的就选择zip的下载。
(7)The Network Adapter could not establish the connection
请检查你的url,user,pwd,dialect等等是否正确
(8)java.lang.NoClassDefFoundError: org/hibernate/cfg/HbmBinder$SecondPass
原因:你更新了hibernate3.2的包,但你的classpath里可能存在hibernate-annotations.jar的包,删除之或更新至最新版本的包即可。
(9)java.lang.IllegalAccessError: tried to access method net.sf.ehcache.CacheManager.<init>()V from class org.hibernate.cache.EhCacheProvider
原因:可能你更新了hiberante的包,导致原来的原来ehcache不行了,原来使用的是1.1的版本,换成1.5以上的正常,另还需要一个javassist.jar包
(10)Connections could not be acquired from the underlying database
大概的意思是说:不能连接当前所配置的数据库,检查你的URL链接字符串是否正确。
(11) org/apache/commons/pool/impl/GenericObjectPool 的解决方法
java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool原因:
找不到 Caused by:
java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
就是Apache所提供的基本对象池。
解决:
到这里http://commons.apache.org/downloads/download_pool.cgi下载包,然后,把里面的commons-pool-x.jar复制到myEclipse的Referenced Libraries下.
(12)错误提示:java.lang.NoClassDefFoundError:
org/apache/commons/collections/LRUMap类没找到,
或org/apache/commons/collections/SequenceHashMap类没找到:
工程里的commons-collections.jar包与其它包不匹配导致的,例如如果你用
commons-beanutils-1.7.0.jar,commons-collections-3.2.jar时会出现你的那种问题,如果是commons-beanutils-1.7.0.jar,commons-collections-3.1.jar就不会就这种问题了...祝你好运啦
(13)Spring2.0与Hibernate3.0的错误解决
严重: action: null
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'sessionfactory' defined in ServletContext resource [/WEB-INF/beans-config.xml]: Initialization of bean failed; nested exception is java.lang.SecurityException:
class "org.apache.commons.collections.SequencedHashMap"'s signer information does not match signer information of other classes in the same package
java.lang.SecurityException: class "org.apache.commons.collections.SequencedHashMap"s signer information does not match signer information of other classes in the same package
at java.lang.ClassLoader.checkCerts(ClassLoader.java:775)
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:487)
at java.lang.ClassLoader.defineClass(ClassLoader.java:614)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124) at
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1817)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872).........
Spring2.0 AOP Liberaries里的asm2.2.3.jar和Hiberate中的生成代理用的asm.jar冲突,后来把asm2.2.3.jar删了才好使
或者
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V
这个错误是因为系统需要一个叫“asm.jar”的包,而这个包在SpingAop包中和Hibernate包中都有,而且不象话的是这两个包中的类org.objectweb.asm.ClassVisitor的方法visit方法签名竟然不一样,所以解决方法很简单,项目在加HIBERNATE3能力的时候不加库,直接用Spring的AOP的库就可以了。
在使用Spring的AOP编程时,会用到这几个lib:
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
Hibernate使用如果lib:
asm.jar
asm-attrs.jar
其中asm-2.2.2.jar与asm.jar存在类上的冲突!
使用其中之一或两者都使用,可能会出现如下错误:
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
java.lang.NoClassDefFoundError: org/objectweb/asm/commons/EmptyVisitor
java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit。。。。。。
解决办法是:
去掉类路径上的关于Hibernate的3个lib
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
保留Spring中的以下4个lib
asm-2.2.2.jar
asm-commons-2.2.2.jar
asm-util-2.2.2.jar
cglib-nodep-2.1_3.jar
或去掉asm-commons-2.2.2.jar
asm-util-2.2.2.jar
cglib-nodep-2.1_3.jar
(14) 错误提示:net.sf.cglib.core.CodeGenerationException:
java.lang.reflect.InvocationTargetException-->null
Caused by: java.lang.reflect.InvocationTargetException
Caused by: java.lang.SecurityException:
class "com.wuwei.struts.bean.User$$EnhancerByCGLIB$$1016ffbf"'s signer information does not match signer information of other classes in the same package
在….hbm.xml文件中,将class标签的lazy属性改为false(默认为true)
<class name="com.wuwei.struts.bean.User" table="user" lazy="false">。如果有schema="dbo" catalog="test"配置信息,请将其删除
(15) 错误提示:java.lang.OutOfMemoryError:Java heap space的解决方法
1、设置环境变量
set JAVA_OPTS= -Xms32m -Xmx512m可以根据自己机器的内存进行更改,但本人测试这种方法并没有解决问题。可能是还有哪里需要设置。
2、java -Xms32m -Xmx800m className
就是在执行JAVA类文件时加上这个参数,其中className是需要执行的确类名。(包括包名)这个解决问题了。而且执行的速度比没有设置的时候快很多。如果在测试的时候可能会用Eclispe 这时候就需要在Eclipse ->run -arguments 中的VM arguments
(16) tomcat5的common/lib目录下jar包和WEB/lib下jar包冲突问题
启动tomcat5,基本正常,打开普通的JSP也正常,但打开与struts相关的jsp报错如下:
Javax.Servlet.ServletException: javax.servlet.jsp.PageContext.handlePageException(Ljava/lang/Throwable;)V
org.apache.jASPer.servlet.JspServlet.service(JspServlet.java:256)
javax.servlet.http.HttpServlet.service(HttpServlet.java:868)
在tomcat/common/lib/中也放入了J2EE.jar等包,去common/lib中处不必要的包后,保证tomcat/common/lib/和WEB-INF/lib这两个目录下的jar没有重复的,问题解决。
原来,tomcat 启动后先将tomcat/common/lib目录下的jar包全部读入内存,如果webapps目录里的应用程序中WEB-INF/lib目录下有相同的包,将无法加载,估计不同版本的包之间也会造成类似问题。因此建议尽量不要把jar包放入tomcat/common/lib目录下,应该放入WEB-INF/lib目录下。
(17) java.lang.NoClassDefFoundError: net/sf/hibernate/Session
后来发现,原来是HibernateDaoSupport类import引用的版本问题,应该与系统中使用的Hibernate框架的版本对应。在此就是
org.springframework.orm.hibernate3.support.HibernateDaoSupport 而不是org.springframework
(18)错误: java.lang.NullPointerException
StandardWrapperValve[action]: Servlet.service() for servlet action threw exception
javax.servlet.jsp.JspException: Cannot retrieve mapping for action /settlementTypeManage
或者: type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
原因: 发现 dao 实例、 manage 实例等需要注入的东西没有被注入
解决:这个时候,你应该查看日志文件;默认是应用服务器的 log 文件,比如 Tomcat 就是 [Tomcat 安装目录 ]/logs ;你会发现提示你: 可能是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sf' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.HibernateException: could not configure from URL: file:src/hibernate.cfg.xml
org.hibernate.HibernateException: could not configure from URL: file:src/hibernate.cfg.xml
……………………….
Caused by: java.io.FileNotFoundException: src\hibernate.cfg.xml
可能是:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Resource: com/mcc/coupon/model/UserRole.hbm.xml not found
org.hibernate.MappingException: Resource: com/mcc/coupon/model/UserRole.hbm.xml not found
然后你就知道原因是因为配置文件的解析出了错误,这个通过 Web 页面是看不出来的。
更多的是持久化映射文件出的错误;导致了没有被解析;当然你需要的功能就无法使用了。
(19) 错误:StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: org.apache.struts.taglib.bean.CookieTei
界面错误具体描述:
org.apache.jasper.JasperException: Failed to load or instantiate TagExtraInfo class: org.apache.struts.taglib.bean.CookieTei
原因与解决:
<方案一>你的”tml:”开头的标签没有放在一个中
<方案二>重新启动你的应用服务器,自动就没有这个问题了
(20) 错误:java.sql.SQLException: Field 'userId' doesn't have a default value
原因:把配置文件的主键改为<generator class="assigned" /> 可以进行自定义主键值,因为我在设主键时没有设成自动增长的,要求填默认值是受了配置文件里<generator class="native"/>的影响了。
(21)错误:org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)
原因与解决:
方法1 删除Set方的cascade
方法2 解决关联关系后,再删除
方法3 在many-to-one方增加cascade 但值不能是none
最后一招:
检查一下hashCode equals是否使用了id作为唯一标示的选项了;我用uuid.hex时是没有问题的;但是用了native,就不行了,怎么办?删除啊!
这个错误可以参见我的blog文章:
http://www.blogjava.net/crazycy/archive/2006/06/24/54939.html
(22)错误:exception javax.servlet.ServletException: BeanUtils.populat root cause
java.lang.IllegalArgumentException:Cannot invoke ***Form.set*** - argument type mismatch
原因:这个问题很奇怪的说,为什么说奇怪呢?主要针对Form是日期型的来说的;因为我做过N多系统Form中都是用java.util.Date,都是没有问题的。
解决:
第一:你把Date换成String;在Action中进行转换;当然转换要借助于SimpleDateFormate方法喽
第二:记得在form中增加enctype="multipart/form-data"
(23)问题:用Tomcat5.5.12,发现原来很好用的系统不能用了,反复测试发现页面中不能包含 taglib,否则会出现以下提示:
HTTP Status 500 -type Exception report
Message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /index.jsp(1,1) Unable to read TLD "META-INF/tlds/struts-bean.tld" from JAR file "file:*****/WEB-INF/lib/struts.jar":
原因:更新了工程用的lib文件夹下的jar,发布时也发布了servlet.jar和jsp-api.jar。
解决:把jsp-api.jar删除就解决这个问题了。
(24)问题:Tomcat5.0.20中差错可以通过[Tomcat安装目录]/logs下的localhost_log.2006-07-14.txt类似的文件看具体的错误日志,但是在5.5中就找不到了
原因与解决:我把[Tomcat安装目录]/bin下的tomcat5w.exe的logging标签捣鼓了一会,然后重起就有了。原因具体说不准,用非安装版也有这个问题。
(25) HTTP Status 404 - Servlet action is not available
type Status report
message Servlet action is not available
description The requested resource (Servlet action is not available) is not available.
这个是因为在struts-config中,action执行交给了spring,
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
</plug-in>
这需要org.springframework.web这个包,我没有导入,action跳转自然出错。解决方法就是导入spring-web.jar这个包。
(26): javax.servlet.ServletException: Error creating bean with name '/login' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'UserDAO' is defined
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.siyue.struts.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:167)
这主要是applicationContext.xml文件中没有声明UserDao这个Bean,加入以下就可以解决。
<bean name="UserDAO" class="com.siyue.hibernate.UserDAO" singleton="false">
</bean>
但是,这就出现了下面的问题:
javax.servlet.ServletException: Error creating bean with name '/login' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'UserDAO' while setting bean property 'userDAO'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserDAO' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: sessionFactory or hibernateTemplate is required
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
com.siyue.struts.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:167)
这是因为声明中没有注入SessionFactory所致,将上面声明的UserDao改成这样就解决了:
<bean name="UserDAO" class="com.siyue.hibernate.UserDAO" singleton="false">
<property name="sessionFactory" ref ="sessionFactory" />
</property>
</bean>
(27)org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
原因是ID在数据库中应该是自增长列,而在User.hbm.xml中却没有定义成自增长列,在插入数据库时,也就是Save()方法时,出现错误。这样改:
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="increment" />
</id>
(28) java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource的解决方法
只要把commons-pool.jar、commons-dbcp-1.2.2.jar和commons-collections-3.2.jar三个jar包加入classpath中就没问题拉!
(29) 出现错误:
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.microsoft.sqlserver.jdbc.SQLServerDriver' for connect URL '
jdbc:sqlserver://localhost;DatabaseName=exam
.............
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:264)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
... 38 more
首先要保证项目中导入了sqljdbc.jar,再有就是注意配置文件导入的类
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
(30)identifier of an instance of my.MyObject altered from N to N
这个异常的产生主要是VO中的类中的属性的类型和你影射文件中属性的类型不能一一对应。比如:ID的属性在类中使用的LONG,但是在配置文件描述却是Integer.结果就是上面的异常了!
(31) org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'useraction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'user_server' of bean class [com.bids.action.UserAction]: Bean property 'user_server' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'user_server' of bean class [com.bids.action.UserAction]: Bean property 'user_server' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:793)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:645)
.........
2009-1-21 12:09:18 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
原因是"user_server""user_pojo"没有get和set方法。
(32) 地址栏中:http://localhost:8088/sshtest/user.do?act=register
报错:HTTP Status 404 - Servlet action is not available
原因是:struts-config.xml配置文件中的请求处理器类出错了(找不到类路径)
<controller processorClass="com.xj.requestprocessor.MyProcessor"></controller>
解决办法:
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
(33)报错 :javax.servlet.ServletException:
Cannot find ActionMappings or ActionFormBeans collection
原因: 工程里面的struts包.有损坏或某些struts包没有存在.
解决办法:重新创建一个工程,并且导入struts,然后将导入的struts包copy到目前的工厂的 WEB-INf/lib目录,重新部署,重新启动服务,就ok了
(34) 报错:java.lang.Long cannot be cast to java.lang.Integer
Long 无法转化成Integer类型. 这个异常 经常出现在hinbernate分页查询的时候.
例如:
注: super.pageQuery(hql,null,null,null);调用了一个父类的一个封装方法.查询时候使用
List list = this.getHibernateTemplate().executeFind(new HibernateCallback(){});
/**
* 查询所有用户记录总数
*/
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
return (Integer) list.get(0);
}
原因:
这里在Hibernate2.0之前版本list.get(0)返回的是Integer类型.
但是在Hibernate3.0以后版本list.get(0)返回的是Long类型.
所以在这里不可以由Long型强转成Integer类型.
解决办法:
public Integer getUsersCount() {
String hql = "select count(*) from Users";
List list = super.pageQuery(hql, null, null, null);
Number num = (Number) list.get(0);
return num.intValue();
}
注:java.lang.Number是Integer,Long的父类.
(35) 报错 : Servlet Action is not available 因为Spring配置文件的位置放在WEB-INF下,要在WEB.XML文件中作为Servlet步署,而不能用插件方式连接
(1). 将struts-config.xml文件中的
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in> 去掉
(2).然后在web.xml文件中加上这段代码就可以了.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
(36) 报错:'sessionFactory' or 'hibernateTemplate' is required
原因: 在dao操作类中需要注入HibernateTemplate来创建sessionFactory. 或者直接注入sessionFactory.
错误的写法:
<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
</bean>
<bean id="userdao" class="com.xj.dao.impl.UsersDao"
lazy-init="true">
</bean>
解决办法: 要将这2个对象中的某一个注入到dao中.
第一种方法:
<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
<property name="sessionFactory" ref ="sessionFactory" />
</property>
</bean>
第二种方法:
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" / >
</bean>
<bean id="depsdao" class="com.xj.dao.impl.DepsDao"
lazy-init="true">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
(37) 报错: java.lang.IllegalStateException: No WebApplicationContext found:
no ContextLoaderListener registered?
原因: web.xml文件中的配置写错了[ 注:对与插件方式不存在这个问题 ]
解决办法:在web.xml中加上
<context-param>
<param-name> contextConfigLocation </param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name> context </servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
(38) 报错:No bean named 'sessionFactory' is defined
错误可能原因1:在web.xml中加载applicationContext.xml文件的时候没有加载成功,看你的路径是否 正确,这个配置错误,服务器启动的时候不会报错
错误可能原因2:没有删除asm-2.2.3.jar文件.一定要直接从磁盘目录删除.然后刷新工程,重新部署 重启服务.
(39) 报错:sessionFactory或者hibernateTemplate找不到,有的时候,我们明明就在dao中注入了sessionFactory或hibernate,但是还是总是报错,说sessionFactory或者hibernateTemplate找不到
下面这种情况会引发这种情况:
在struts中的formbean中的reset方法,往往我们用来初始化界面的一些显示值.
如一个注册,可能在这里先查询数据库,找到所有的部门信息,然后绑定在界面的下拉框中.所以可能会在reset方法中调用业务层方法.(也有可能请求页面之前,先经过一个servlet或jsp,在servlet里面调用业务层方法)
如果你在formbean中是这样写会报上面的错误:
ApplicationContext ap = new FileSystemXmlApplicationContext(
new String[] {
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\applicationContext.xml",
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\operatorbeans.xml",
"E:\\MyWorkPlace\\sshtest\\WebRoot\\WEB-INF\\actionbeans.xml" });
IUserService us = (IUserService) ap.getBean("userservice");
然后用us 直接调用方法.
原因: 在这里ApplicationContext ap = new FileSystemXmlApplicationContext(
new String[] {});
启动的实际上是另外一个spring容器,而不是刚刚启动服务的时候创建的那个spring容器.
解决办法:
在reset方法中:
ServletContext context = servlet.getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute
(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
IDeptService ds = (IDeptService) app.getBean("deptservice");
this.depList = ds.selectAllDeps();
注:servlet是struts框架中间的一个对象,保存了web容器的很多信息
那么之前是怎样设置进去的呢?
// context.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,启动的时候创建的那个spring器);
(40) 报错: org.apache.jasper.JasperException: $Proxy3 cannot be cast to
com.xj.service.impl.RoleService
java.lang.ClassCastException: $Proxy3 cannot be cast to com.xj.service.impl.RoleService
原因:很明显又是一个代理时候,强转错误:
ServletContext context = servlet.getServletContext();
ApplicationContext app = (ApplicationContext) context.getAttribute
(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
IDeptService ds = (DeptService) app.getBean("deptservice");
this.depList = ds.selectAllDeps();
就是这段示例代码中的 IDeptService ds = (DeptService) app.getBean ("deptservice")出错了不能直接强转成接口的实现类,一定要转成接口.
IDeptService ds = (IDeptService) app.getBean("deptservice");
(41) org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save():
原因:<id>元素配置不正确,<id>元素缺少其子元素<generator></generator>的配置。解决方法:<id>元素映射了相应数据库表的主键字段,对其子元素<generator class="">,其中class的取值可以为increment、identity、sequence、hilo、native......等,一般取其值为native 。
(42) java.lang.IllegalArgumentException: id to load is required for loading
方案:Users user=(Users)this.getHibernateTemplate().get(Users.class, id);如果以用户名为参数不能用此方法
(43) org.springframework.dao.InvalidDataAccessResourceUsageException:
could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
方案:sql语句有错误
(44) 严重: Exception sending context initialized event to listener instance of class com.sun.faces.config.ConfigureListener
javax.faces.FacesException: Can't parse configuration
file:jar:file:/E:/工具/apache-tomcat-6.0.9/apache-tomcat-6.0.9/webapps/jsf02/
WEB-INF/lib/jsf-impl.jar!/com/sun/faces/standard-html-renderkit.xml
方案:把tomcat的安装文件安装到英文目录下
(45) spring MVC+hibernate
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; nested exception is
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
Caused by: org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.transaction.interceptor.TransactionProxyFactoryBean] for bean with name 'userService' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org/springframework/aop/framework/AbstractSingletonProxyFactoryBean Caused by:
java.lang.NoClassDefFoundError: org/springframework/aop/framework/AbstractSingletonProxyFactoryBean
方案:添加 spring 2.0 aop libraries
(46)org.apache.jasper.JasperException: Unable to compile class for JSP:
或org.apache.jasper.JasperException: Unable to compile class for JSP
方案:jsp网页中的java代码有问题
或:jsp页面中的java代码括号不对称!
(47)错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CompanyController' defined in class path resource [com/company/test/company-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'methodNameResolver' of bean class [com.company.action.CompanyController]: Bean property 'methodNameResolver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'methodNameResolver' of bean class [com.company.action.CompanyController]: Bean property 'methodNameResolver' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
控制器没继承MultiActionController
(48)提示:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'turDiffService' defined in class path resource [com/expert/test/expert-config.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'turDifficultDao' of bean class [com.expert.operation.impl.TurDiffSerImpl]: Bean property 'turDifficultDao' is not writable or has an invalid setter method. Did you mean 'truDifficultDao'?
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'turDifficultDao' of bean class [com.expert.operation.impl.TurDiffSerImpl]: Bean property 'turDifficultDao' is not writable or has an invalid setter method. Did you mean 'truDifficultDao'?
原因:在定义时TurDifficultDao turDifficultDao ,把有个字母写错,后发现错误,改过来了,但没把get() set()方法中对应的改过来
(49)错误:Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'urlMapping' defined in class path resource [com/expert/test/expert-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '' is defined
原因:在expert-config.xml中创建<bean
id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key=""></prop>
</props>
</property>
</bean>
没有给<prop key=""></prop>赋值
(50)org.springframework.orm.hibernate3.HibernateQueryException: Instruction is not mapped
[from Instruction]; nested exception is org.hibernate.hql.ast.QuerySyntaxException:
Instruction is not mapped [from Instruction]
Caused by: org.hibernate.hql.ast.QuerySyntaxException: Instruction is not mapped [from
Instruction]
原因:在data-config.xml中没加入<value>com/serializ/Instruction.hbm.xml</value>
(51) 项目的启动问题:
信息: ExtensionValidator[/******][commons-attributes-compiler.jar]: Required extension "ant " not found.
2007-2-13 8:43:59 org.apache.catalina.util.ExtensionValidator validateManifestResources
信息: ExtensionValidator[/******][commons-attributes-compiler.jar]: Required extension "commons-attributes-api " not found.
2007-2-13 8:43:59 org.apache.catalina.util.ExtensionValidator validateManifestResources
信息: ExtensionValidator[/******][commons-attributes-compiler.jar]: Required extension "javadoc " not found.
2007-2-13 8:43:59 org.apache.catalina.util.ExtensionValidator validateManifestResources
信息: ExtensionValidator[/******]: Failure to find 3 required extension(s).
尝试以下方法: 直接移除commons-attributes-compiler.jar包; 注意查看WEB-INF/lib下有没有,一并移除;
JSP的启动问题:
Console错误信息:
信息: validateJarFile(*******/WEB-INF/lib/servlet-api.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
javax.servlet.ServletContext在servlet-api.jar包里,tomcat lib文件夹里有,奇怪我这都包含在classpath中还报告找不到,后来重新添加了下才ok;
DWR的启动问题:
<create javascript="PiupiupiuUserService" creator="spring">
<param name="beanName" value="piupiupiuUserService"></param>
<param name="location" value="applicationContext.xml"/>
</create>
beanName的value是spring配置文件applicationContext.xml中配置指定的, create的子元素location以前没用过,因为有次warning报告找不到bean,就查了下官网documention,加上了这个,指定为applicationContext.xml好了.. 不过启动的过程中,在console却出现了:
WARN [http-8080-1] util.LocalUtil –
No property 'location' on org.directwebremoting.spring.SpringCreator
INFO [http-8080-1] spring.SpringCreator –
Spring BeanFactory via ClassPathXmlApplicationContext using 1configLocations.
=2008-04-04 08:43:05,9809209
去掉了就会出现 No WebApplicationContext found: no ContextLoaderListener registered?
还好没什么影响,dwr能找到spring配置文件就好~
一些常用jar包作用
commons-digester.jar |
错误
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
没有设置lazy属性