问题描述
大家帮我看看我Dao和Service都用了基类,都通用的还正常,非通用类写在专属service出错,写在action正常。BarepackageUploadAction调用BarepackageServiceImpl这个类的updatePhoto(StringphotoFileName,Integerid,IntegerphotoSn)就会报错,把这个函数的内容直接写到action里就正常。错误信息:org.hibernate.HibernateException:NoSessionfoundforcurrentthreadorg.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:990)com.nondelivery.dao.impl.BaseDaoImpl.getSession(BaseDaoImpl.java:37)com.nondelivery.dao.impl.BaseDaoImpl.findById(BaseDaoImpl.java:41)com.nondelivery.service.impl.BaseServiceImpl.findById(BaseServiceImpl.java:38)com.nondelivery.service.impl.BarepackageServiceImpl.updatePhoto(BarepackageServiceImpl.java:37)sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)java.lang.reflect.Method.invoke(Method.java:597)org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)$Proxy18.updatePhoto(UnknownSource)packagecom.nondelivery.action;importjava.io.*;importjava.util.Date;importorg.apache.struts2.ServletActionContext;importorg.apache.struts2.json.annotations.JSON;importorg.springframework.stereotype.Component;//importcom.google.gson.JsonArray;//importcom.google.gson.JsonObject;//importcom.google.gson.JsonElement;//importcom.google.gson.JsonPrimitive;//importcom.google.gson.JsonPrimitive;importcom.nondelivery.service.IBarepackageService;importcom.nondelivery.vo.Barepackage;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;importcom.zym.util.StrArrayTransform;importcom.zym.util.StrInclueFixedSeparator;//importentity.Driver;//importentity.Driver;@Component("barepackageUploadAction")publicclassBarepackageUploadActionextendsActionSupport{privatestaticfinallongserialVersionUID=1L;IBarepackageServicebarepackageService;//privateBarepackagebarepackage;privateIntegerid;//邮件序号privateStringphotoSn;//照片顺序号privateStringrelativePhotoDir="uploaderphoto";privateStringfieldPhoto;//判断是否更新成功privatebooleansuccess;privateStringmsg="";privateFileupload;privateStringuploadContentType;//这里要注意一点,uploadFileName中的upload要与传入过来的File名字一样,不然获取不到文件的名字,今天就因为这里不一致,找了一上午没找到原因privateStringuploadFileName;privatestaticfinalintBUFFER_SIZE=16*1024;publicStringexecute()throwsException{Dated=newDate();//this.setUploadContentType("text/html");System.out.println("uploadFileName="+this.uploadFileName);//要上传的文件名//String[]tmpFileType1=StrArrayTransform.StringToArray(this.uploadFileName,".");StringtmpFileType=uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);StringphotoFileName=this.id.toString()+"_"+this.photoSn+"."+tmpFileType;//服务器端相片文件名称,此文件名按一定次序存入数据库this.uploadFileName=photoFileName;//获得wapps的路径,uploader指的放在文件的地方StringtoSrc=ServletActionContext.getServletContext().getRealPath(relativePhotoDir)+"\"+photoFileName;System.out.println("toFile="+toSrc);FiletoFile=newFile(toSrc);this.success=writeFile(this.upload,toFile);if(this.success){barepackageService.updatePhoto(photoFileName,this.id,Integer.parseInt(this.photoSn));Barepackaget=barepackageService.findById(id);this.fieldPhoto=t.getPhoto();this.msg="照片上传成功!";}else{this.msg="照片上传失败";}returnSUCCESS;}privatestaticbooleanwriteFile(Filesrc,Filedst){System.out.println("==文件寫入==");try{InputStreamin=null;OutputStreamout=null;try{in=newBufferedInputStream(newFileInputStream(src),//获得要上传的文件BUFFER_SIZE);out=newBufferedOutputStream(newFileOutputStream(dst),//指定要上传到的位置BUFFER_SIZE);byte[]buffer=newbyte[BUFFER_SIZE];//开始写入while(in.read(buffer)>0){out.write(buffer);}}finally{if(null!=in){in.close();}if(null!=out){out.close();}}}catch(Exceptione){e.printStackTrace();returnfalse;}System.out.println("写入成功!");returntrue;}}packagecom.nondelivery.service.impl;importjava.util.List;importjavax.annotation.Resource;importorg.springframework.stereotype.Component;importorg.springframework.stereotype.Service;importcom.nondelivery.service.impl.BaseServiceImpl;importcom.nondelivery.dao.IBarepackageDao;importcom.nondelivery.dao.IBaseDao;importcom.nondelivery.dao.IUserDao;importcom.nondelivery.service.IBarepackageService;importcom.nondelivery.vo.Barepackage;importcom.zym.util.StrArrayTransform;importcom.zym.util.StrInclueFixedSeparator;@Component("barepackageService")publicclassBarepackageServiceImplextendsBaseServiceImpl<Barepackage,Integer>implementsIBarepackageService{privateIBarepackageDaobarepackageDao;@OverridepublicIBaseDao<Barepackage,Integer>getDao(){returnthis.barepackageDao;}publicIBarepackageDaogetBarepackageDao(){returnbarepackageDao;}@Resource(name="barepackageDao")publicvoidsetBarepackageDao(IBarepackageDaobarepackageDao){this.barepackageDao=barepackageDao;}publicvoidupdatePhoto(StringphotoFileName,Integerid,IntegerphotoSn){Barepackaget=findById(id);String[]strArray=StrArrayTransform.StringToArray1(StrInclueFixedSeparator.StringInclueFixedSeparator(t.getPhoto(),","),",");strArray[photoSn-1]=photoFileName;t.setPhoto(StrInclueFixedSeparator.StringInclueFixedSeparator(StrArrayTransform.arrayToString(strArray),","));update(t);}}packagecom.nondelivery.service.impl;importjava.io.Serializable;importjava.util.List;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Propagation;importorg.springframework.transaction.annotation.Transactional;importcom.nondelivery.dao.IBaseDao;importcom.nondelivery.service.IBaseService;@ServicepublicabstractclassBaseServiceImpl<T,IDextendsSerializable>implementsIBaseService<T,ID>{publicabstractIBaseDao<T,ID>getDao();public<T>voidadd(Tt){getDao().save(t);}public<T>voiddelete(Tt){getDao().delete(t);}public<T>voiddelete(IDid){getDao().delete(id);}@Transactional(readOnly=true,propagation=Propagation.REQUIRED)public<T>List<T>findAll(){List<T>list=getDao().findAll();returnlist;//returngetDao().findAll();}publicTfindById(IDid){returngetDao().findById(id);}public<T>voidupdate(Tt){getDao().update(t);}public<T>List<T>findByHql(Stringhql){returngetDao().findByHql(hql);}public<T>List<T>findByHql(Stringhql,Objectparam){returngetDao().findByHql(hql,param);}public<T>List<T>findByHql(Stringhql,Object[]params){returngetDao().findByHql(hql,params);}}packagecom.nondelivery.dao.impl;importjava.util.List;importorg.hibernate.Session;importorg.springframework.stereotype.Component;importcom.zym.util.StrArrayTransform;importcom.nondelivery.dao.impl.BaseDaoImpl;importcom.nondelivery.dao.IBarepackageDao;importcom.nondelivery.vo.Barepackage;@Component("barepackageDao")publicclassBarepackageDaoImplextendsBaseDaoImpl<Barepackage,Integer>implementsIBarepackageDao{publicBarepackageDaoImpl(){super(Barepackage.class);//TODOAuto-generatedconstructorstub}publicvoidupdatePhoto(StringphotoFileName,Integerid,IntegerphotoSn){//Barepackaget=(Barepackage)findById(id);//String[]strArray=StrArrayTransform.StringToArray(t.getPhoto(),",");//strArray[photoSn-1]=photoFileName;//t.setPhoto(StrArrayTransform.arrayToString(strArray));//this.update(t);}}packagecom.nondelivery.dao.impl;importjava.io.Serializable;importjava.util.List;importjavax.annotation.Resource;importorg.apache.commons.logging.Log;importorg.hibernate.Query;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importcom.nondelivery.dao.IBaseDao;//importcom.drivers.vo.*;/****@authorZouTao**Sep3,2013*@param<T>*/publicclassBaseDaoImpl<T,IDextendsSerializable>implementsIBaseDao<T,ID>{protectedSessionFactorysessionFactory;privateClass<T>cls;publicBaseDaoImpl(Class<T>cls){this.cls=cls;}@Resource(name="sessionFactory")publicvoidsetSessionFactory(SessionFactorysessionFactory){this.sessionFactory=sessionFactory;}publicSessiongetSession(){returnthis.sessionFactory.getCurrentSession();//从3.0.1开始,hibernate增加了sessionFactory.getCurrentSession()方法,采用getCurrentSession()创建的session在commit或rollback时会自动关闭}public<T>TfindById(IDid){return(T)getSession().get(this.cls,id);}public<T>voidsave(Tt){getSession().save(t);}public<T>voidupdate(Tt){getSession().update(t);}public<T>voiddelete(Tt){getSession().delete(t);}public<T>voiddelete(IDid){getSession().delete(findById(id));}}
解决方案
解决方案二:
第一种:吧getCunrrenThread()改为openSession();第二种:配置currenThread的环境,在hibernate.cfg.xml中,加上session在上下文中取值,就行了,具体没记住,有两个取值,一个是jta范围(跨数据库平台),一个Thread_Current...忘了。应该是这个问题
解决方案三:
楼上说的很对,可我还不是很明白,也就是我的ssh知识有限不会变通。我用的是hibernate4、spring4,要不我把整个类包发给你你帮我改下,我只是想实现:BarepackageDaoImpl继承BaseDaoImpl通用方法的前提下能写自己专属的方法比如此处的updatePhoto(StringphotoFileName,Integerid,IntegerphotoSn)方法。可以吗?可以另开贴把我的所有430都给你,改好了也可以付费谢谢
解决方案四:
我用extjs4.2mvc做过一个单位项目你若需要可以拿去谢谢