总结最近遇到的一些异常处理

时间:2023-01-26 08:24:02

 一个Hibernate异常:

在 我的SalesDAOImpl中的
public PaginationSupport getSalesPage(int pageSize, int startIndex,Integer orgId) throws DataAccessException 方法中遇到了下面的异常报错!
[WARN ] 2010-11-18 11:34:16,359 - org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 24000
[ERROR] 2010-11-18 11:34:16,359 - org.hibernate.util.JDBCExceptionReporter - ResultSet may only be accessed in a forward direction.
org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
在网上搜索一番,发现我穿的参数(startIndex)值的问题。
q.setFirstResult(startIndex);
在sqlserver中,这个(startIndex)不能为负数。如果为负数回报上面的异常错误。
 
 
在update操作的时候遇到了这样的错误  
org.hibernate.TransientObjectException: The given object has a null identifier: com.dbwen.handsale.pojo.Sales
意思  是说update的 实体对象的的主键值 NULL  没有   报错
后来检查的时候  是发现了   id值 为NULL ,赋值修改后,一切ok!
所以我个人感觉如果update有问题,看看你 update的对象的主键的值是否存在!
 
 
一个Dwr异常:
[WARN ] 2010-11-19 11:25:51,390 - org.directwebremoting.dwrp.BaseCallMarshaller - Marshalling exception
org.directwebremoting.extend.MarshallException: Error marshalling com.dbwen.handsale.dto.VisitPlanDto: null. See the logs for more details.
at org.directwebremoting.impl.PropertyDescriptorProperty.setValue(PropertyDescriptorProperty.java:89)
at org.directwebremoting.convert.BasicObjectConverter.convertInbound(BasicObjectConverter.java:140)
注意上面 这样的一句:org.directwebremoting.extend.MarshallException: Error marshalling com.dbwen.handsale.dto.VisitPlanDto: null
怎么可能出现这样的问题了 
var planId=null;
var o ={ planId: planId,
empNo: $('#empNo').val(),
planName: $('#planName').val(),
pathId:$('#pathId').val(),
visitAmount: $('#visitAmount').val(),
planDate: toDate($('#planDate').val()),
memo: $('#memo').val()};
if(action == 'add' && confirm('确认添加拜访计划?')){
$("#loading").show();
var rtn=dbwtool.callDWRBusiness('VisitPlanDWR.addVisitPlan',[o]);。。。。
我在ff浏览器中 断点调试了  此时的o变量中的值  都有 并且我在VisitPlanDwr.java中的addVisitPlan(VisitPlanDto dto)打了断点 ,可是程序根本没有跑到这里。
然后我看看我的dwr.xml(Spring整合Dwr)文件-》关于spring整合Dwr 这里不作详细说明 这里有一篇Blog很详细:  http://eyeone.javaeye.com/blog/46624
<create creator="spring" javascript="VisitPlanDWR" scope="application">
<param name="beanName" value="visitPlanDWR"/>
</create>
<convert converter="bean" match="com.dbwen.handsale.dto.*"/>
<convert converter="bean" match="com.dbwen.handsale.pojo.*"/>
和jsp文件
<script type="text/javascript" src="<%=ctxPath%>/js/visit/visitPlan.js"></script>
<script type='text/javascript' src="<%=ctxPath%>/dwr/interface/VisitPlanDWR.js"></script>
<script type='text/javascript' src="<%=ctxPath%>/dwr/engine.js"></script>
和Spring 中对应的beanId
<bean id="visitPlanDWR" class="com.dbwen.handsale.web.dwr.visit.VisitPlanDWR">
         <property name="visitPlanService" ref="visitPlanService"/>
</bean>
一切没有问题。后来想了想  有可能出现在hibernate的映射文件的问题
 <id name="planId" type="int">
            <column name="plan_id" />
             <generator class="identity" />
 </id>
并且VisitPlan.java文件中的 private int planId;
在添加的时候,一般的主键的值是没有的  他是自动标识的。我在 visitPlan.js中 初始化了  plan_id=null ,可能是dwr对于此int  null转化不了。
int  只是一个数据类型 不是一个对象 。
后来我尝试把 映射文件和.java文件对应的字段改为了  int-》integer
就没有出现此问题,看来是数据类型不匹配。
还有一种方法(不修改planId的  类型还是 int ),有点繁琐:
if(action='add'){
var o ={                 //planId: planId,在添加的时候  不赋值给planId 这样在Dwr与javaBean转化的时候  会在执行添加的时候  自动标识的。
empNo: $('#empNo').val(),
planName: $('#planName').val(),
pathId:$('#pathId').val(),
visitAmount: $('#visitAmount').val(),
planDate: toDate($('#planDate').val()),
memo: $('#memo').val()};
       }
if(action='edit'){
    var o ={             planId: planId,//这时候在运用这一句,但是此时的planId有值从一个方法中获取的,在赋值就不会报错
empNo: $('#empNo').val(),
planName: $('#planName').val(),
pathId:$('#pathId').val(),
visitAmount: $('#visitAmount').val(),
planDate: toDate($('#planDate').val()),
memo: $('#memo').val()};
           }
 
但由此可见,添加了代码,有点冗长。个人感觉还是int-》integer好点!
 
一个小细节:
一般我们在获取数据库的List对象的时候,用for循环去做例如:
                        list =q.list();
for(int i=0;i<list.size();i++){
Object[] objs= (Object[] )list.get(i);
VisitPlanDto dto = new VisitPlanDto();
PropertyUtils.copyProperties(dto,objs[0]);
dto.setPathName((String)objs[1]);
dto.setPlaceCollection((String)objs[2]);
dto.setLatLanCollection((String)objs[3]);
result.add(dto);
}
但是objs[i].toString 在处理null的过程中  会发生转化错误
而(String)objs[2]  则不会出现
所以在从数据中获取的数据需要相应的转化的时候 直接在objs[i]前面加一个 要转化的类型。