spring - In Java is Exception is not handled in try catch id there is an if else in try block -
i new hibernate , springs, developing project unable understand why try catch block not working.
the project structure follows: jsp > controllers > services > dao
i have bean named seller necessary fields in it, passed seller controller making update in database follows:
@requestmapping(value = "/updateseller") public string updatesellerprofile(model model, httpsession session, sellerformbean seller) { int action = seller.getaction(); try { if (action == 1 || action == 2) { //this modify seller profile giving exception of dataintegrityviolationexception: not-null property references null or transient value seller = sellerservice.modifysellerprofile(seller); model.addattribute("countries", commonservice.getcountrydropdown()); } if (action == 3 || action == 4) { sellerservice.modifycasestudies(seller.getsellerid(), seller.getcasestudies()); } integer sellerid = (integer) session.getattribute("entityid"); sellerformbean bean=null; bean = sellerservice.retrieveseller(sellerid); long userid = (long) session.getattribute("userid"); try { userformbean user = userservice.getuser(userid); bean.setuser(user); } catch (illegalaccessexception | invocationtargetexception e) { log.error("sellercontroller - updateseller , getuser failed ",e); } model.addattribute("seller", bean); model.addattribute("message", "profile updated successfully"); }catch(illegalstateexception e){ log.error("sellercontroller - updateseller : ",e); } catch (nullpointerexception |hibernateexception e) { log.error("sellercontroller - updateseller",e); model.addattribute("error", "error occured while updating profile. please try again later. if problem persist, please conctact system administrator"); }catch(exception e) { log.error("sellercontroller - updateseller",e); model.addattribute("error", "error occured while updating profile. please try again later. if problem persist, please conctact system administrator"); } return selecttab(action); }
now know error , how correct error problem when there exception raised in method modifysellerprofile exception not caught within try cath block of controller. reason?
but when use in controller exception handled:
try { //this modify seller profile giving exception of dataintegrityviolationexception: not-null property references null or transient value seller = sellerservice.modifysellerprofile(seller); }catch (exception e) { log.error("hello error caught you",e); }
the modifysellerprofile follows:
public void modifysellerprofile(seller instance){ log.debug("attaching dirty seller instance"); seller sellerdo = new seller(); beanutils.copyproperties(formbean, sellerdo, "casestudies", "references", "accelerators"); ///====deliberately created exception testing instance.setcompanytype(null); sessionfactory.getcurrentsession().saveorupdate(instance); log.debug("attach successful"); }
stack trace :
<code>[http-bio-8080-exec-7] debug org.springframework.orm.hibernate4.hibernatetransactionmanager - exposing hibernate transaction jdbc transaction [com.mysql.jdbc.jdbc4connection@fa47468] [http-bio-8080-exec-7] debug com.htss.market.dao.sellerhome - attaching dirty seller instance [http-bio-8080-exec-7] debug com.htss.market.dao.sellerhome - attach successful [http-bio-8080-exec-7] debug com.htss.market.dao.userhome - getting user instance id: 72 hibernate: select user0_.user_id user_id1_21_0_, user0_.first_name first_na2_21_0_, user0_.middle_name middle_n3_21_0_, user0_.last_name last_nam4_21_0_, user0_.title title5_21_0_, user0_.photo photo6_21_0_, user0_.phone_no phone_no7_21_0_, user0_.email_id email_id8_21_0_, user0_.country country9_21_0_, user0_.password passwor10_21_0_, user0_.created_by created11_21_0_, user0_.created_on created12_21_0_, user0_.updated_by updated13_21_0_, user0_.updated_on updated14_21_0_, user0_.last_login last_lo15_21_0_, user0_.active active16_21_0_, user0_.country_code country17_21_0_ user user0_ user0_.user_id=? [http-bio-8080-exec-7] debug com.htss.market.dao.userhome - successful, instance found [http-bio-8080-exec-7] debug com.htss.market.dao.userhome - attaching clean user instance [http-bio-8080-exec-7] debug com.htss.market.dao.userhome - attach successful [http-bio-8080-exec-7] debug org.springframework.orm.hibernate4.hibernatetransactionmanager - initiating transaction commit [http-bio-8080-exec-7] debug org.springframework.orm.hibernate4.hibernatetransactionmanager - committing hibernate transaction on session [sessionimpl(persistencecontext[entitykeys=[entitykey[com.htss.market.dao.seller#37], entitykey[com.htss.market.dao.user#72]],collectionkeys=[collectionkey[com.htss.market.dao.user.userotps#72], collectionkey[com.htss.market.dao.user.passwordpolicies#72], collectionkey[com.htss.market.dao.user.userentitymappings#72]]];actionqueue[insertions=org.hibernate.engine.spi.executablelist@8d70e98 updates=org.hibernate.engine.spi.executablelist@d6e1acb deletions=org.hibernate.engine.spi.executablelist@7e36f2ca orphanremovals=org.hibernate.engine.spi.executablelist@4e5a4433 collectioncreations=org.hibernate.engine.spi.executablelist@7ec930ae collectionremovals=org.hibernate.engine.spi.executablelist@38cd5fe collectionupdates=org.hibernate.engine.spi.executablelist@4ae5d7e7 collectionqueuedops=org.hibernate.engine.spi.executablelist@56826954 unresolvedinsertdependencies=unresolvedentityinsertactions[]])] [http-bio-8080-exec-7] debug org.springframework.orm.hibernate4.hibernatetransactionmanager - initiating transaction rollback after commit exception org.springframework.dao.dataintegrityviolationexception: not-null property references null or transient value : com.htss.market.dao.seller.companytype; nested exception org.hibernate.propertyvalueexception: not-null property references null or transient value : com.htss.market.dao.seller.companytype @ org.springframework.orm.hibernate4.sessionfactoryutils.converthibernateaccessexception(sessionfactoryutils.java:182) @ org.springframework.orm.hibernate4.hibernatetransactionmanager.converthibernateaccessexception(hibernatetransactionmanager.java:683) @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:565) @ org.springframework.transaction.support.abstractplatformtransactionmanager.processcommit(abstractplatformtransactionmanager.java:757) @ org.springframework.transaction.support.abstractplatformtransactionmanager.commit(abstractplatformtransactionmanager.java:726) @ org.springframework.transaction.interceptor.transactionaspectsupport.committransactionafterreturning(transactionaspectsupport.java:478) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:272) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:95) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.cglibaopproxy$dynamicadvisedinterceptor.intercept(cglibaopproxy.java:655) @ com.htss.service.sellerservice$$enhancerbyspringcglib$$d68f2f19.modifysellerprofile(<generated>) @ com.htss.web.controller.sellercontroller.updatesellerprofile(sellercontroller.java:230) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:498) @ org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:221) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:136) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:114) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:827) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:738) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:85) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:963) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:897) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:970) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:872) @ javax.servlet.http.httpservlet.service(httpservlet.java:647) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:846) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.sitemesh.webapp.contentfilter.contentbufferingfilter.bufferandpostprocess(contentbufferingfilter.java:169) @ org.sitemesh.webapp.contentfilter.contentbufferingfilter.dofilter(contentbufferingfilter.java:126) @ org.sitemesh.webapp.sitemeshfilter.dofilter(sitemeshfilter.java:120) @ org.sitemesh.config.configurablesitemeshfilter.dofilter(configurablesitemeshfilter.java:163) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:51) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:316) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:126) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:90) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:114) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:122) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:111) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:168) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:48) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:205) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:120) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.header.headerwriterfilter.dofilterinternal(headerwriterfilter.java:64) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:91) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:53) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:330) @ org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:213) @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:176) @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:346) @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:262) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:100) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1041) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:603) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1142) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:617) @ java.lang.thread.run(thread.java:748) caused by: org.hibernate.propertyvalueexception: not-null property references null or transient value : com.htss.market.dao.seller.companytype @ org.hibernate.engine.internal.nullability.checknullability(nullability.java:106) @ org.hibernate.event.internal.defaultflushentityeventlistener.scheduleupdate(defaultflushentityeventlistener.java:309) @ org.hibernate.event.internal.defaultflushentityeventlistener.onflushentity(defaultflushentityeventlistener.java:160) @ org.hibernate.event.internal.abstractflushingeventlistener.flushentities(abstractflushingeventlistener.java:231) @ org.hibernate.event.internal.abstractflushingeventlistener.flusheverythingtoexecutions(abstractflushingeventlistener.java:102) @ org.hibernate.event.internal.defaultflusheventlistener.onflush(defaultflusheventlistener.java:55) @ org.hibernate.internal.sessionimpl.flush(sessionimpl.java:1222) @ org.hibernate.internal.sessionimpl.managedflush(sessionimpl.java:425) @ org.hibernate.engine.transaction.internal.jdbc.jdbctransaction.beforetransactioncommit(jdbctransaction.java:101) @ org.hibernate.engine.transaction.spi.abstracttransactionimpl.commit(abstracttransactionimpl.java:177) @ org.springframework.orm.hibernate4.hibernatetransactionmanager.docommit(hibernatetransactionmanager.java:557) ... 83 more </code>
try putting check dataintegrityviolationexception
in dao layer. because it'll caught there or hibernate handle/throw it. never reaches service layer. because of part of code in sessionfactoryutils
if (ex instanceof propertyvalueexception) { return new dataintegrityviolationexception(ex.getmessage(), ex); }
do :
try { //some code } catch(dataintegrityviolationexception e) { log.error("data integrity violation exception "+e); }
Comments
Post a Comment