hibernate - Autowired Service in Validation and Controller cannot save -
i use spring mvc
+ hibernate
+ spring security
.
my problem cannot save data in database when use checkuserexistvalidator autowired userservice. use autowired userservice in controller to. without checkuserexistvalidator work good.
i think cant use 2 transactional @ same time;
checkuserexistvalidator
public class checkuserexistvalidator implements constraintvalidator<checkuserexist, string>{ @autowired userservice userservice; @override public void initialize(checkuserexist checkuserexist) { } @override public boolean isvalid(string login, constraintvalidatorcontext context) { system.out.println("call valid " + userservice); //when in jsp forms occurs error call //first, when no error call second after //userservice in controller , null if(userservice.findlogin(login) != null){ return false; } return true; } }
userserviceimpl
@service("userserviceimpl") public class userserviceimpl implements userservice { @autowired userdao userdao; @override @transactional(readonly = true) public user findlogin(string login) { return this.userdao.findlogin(login); } @override @transactional public void saveuser(user user) { this.userdao.saveuser(user); } }
userdaoimpl
@repository("userdao") public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; @suppresswarnings("unchecked") @override public user findlogin(string login) { session session = this.sessionfactory.getcurrentsession(); list<user> user = new arraylist<user>(); user = session .createquery(" user login = :login") .setparameter("login", login).list(); if(user.size() > 0){ return user.get(0); }else{ return null; } } @override public void saveuser(user user) { session session = this.sessionfactory.getcurrentsession(); session.save(user); } }
registrationcontroller
@controller public class registrationcontroller { @autowired @qualifier("userserviceimpl") userservice userservice; @modelattribute("user") public user user(){ return new user(); } @requestmapping(value = "/registration", method = requestmethod.get) public string registrationpageget(@modelattribute user user){ return "authorization/registration"; } @requestmapping(value = "/registration", method = requestmethod.post) public string registrationpagepost(@modelattribute @valid user user, bindingresult result){ if(result.haserrors()){ return "authorization/registration"; }else{ system.out.println("call controller " + userservice); userservice.saveuser(user); //when in jsp forms occurs error call //second, when no error call first before //userservice in checkuserexistvalidator } return "redirect:/"; } }
and exception
severe: servlet.service() servlet [appservlet] in context path [/musicplayer] threw exception [request processing failed; nested exception javax.validation.validationexception: hv000028: unexpected exception during isvalid call.] root cause java.lang.nullpointerexception @ adrian.example.musicplayer.validation.uservalidation.checkuserexistvalidator.isvalid(checkuserexistvalidator.java:23) @ adrian.example.musicplayer.validation.uservalidation.checkuserexistvalidator.isvalid(checkuserexistvalidator.java:1) @ org.hibernate.validator.internal.engine.constraintvalidation.constrainttree.validatesingleconstraint(constrainttree.java:283) @ org.hibernate.validator.internal.engine.constraintvalidation.constrainttree.validateconstraints(constrainttree.java:133) @ org.hibernate.validator.internal.engine.constraintvalidation.constrainttree.validateconstraints(constrainttree.java:91) @ org.hibernate.validator.internal.metadata.core.metaconstraint.validateconstraint(metaconstraint.java:84) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraint(validatorimpl.java:525) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraintsfordefaultgroup(validatorimpl.java:465) @ org.hibernate.validator.internal.engine.validatorimpl.validateconstraintsforcurrentgroup(validatorimpl.java:429) @ org.hibernate.validator.internal.engine.validatorimpl.validateincontext(validatorimpl.java:381) @ org.hibernate.validator.internal.engine.validatorimpl.validate(validatorimpl.java:184) @ org.hibernate.cfg.beanvalidation.beanvalidationeventlistener.validate(beanvalidationeventlistener.java:137) @ org.hibernate.cfg.beanvalidation.beanvalidationeventlistener.onpreinsert(beanvalidationeventlistener.java:95) @ org.hibernate.action.internal.entityidentityinsertaction.preinsert(entityidentityinsertaction.java:214) @ org.hibernate.action.internal.entityidentityinsertaction.execute(entityidentityinsertaction.java:92) @ org.hibernate.engine.spi.actionqueue.execute(actionqueue.java:490) @ org.hibernate.engine.spi.actionqueue.addresolvedentityinsertaction(actionqueue.java:195) @ org.hibernate.engine.spi.actionqueue.addinsertaction(actionqueue.java:179) @ org.hibernate.engine.spi.actionqueue.addaction(actionqueue.java:214) @ org.hibernate.event.internal.abstractsaveeventlistener.addinsertaction(abstractsaveeventlistener.java:324) @ org.hibernate.event.internal.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:288) @ org.hibernate.event.internal.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:194) @ org.hibernate.event.internal.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:125) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:209) @ org.hibernate.event.internal.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:55) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:194) @ org.hibernate.event.internal.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:49) @ org.hibernate.event.internal.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:90) @ org.hibernate.internal.sessionimpl.firesave(sessionimpl.java:715) @ org.hibernate.internal.sessionimpl.save(sessionimpl.java:707) @ org.hibernate.internal.sessionimpl.save(sessionimpl.java:702) @ adrian.example.musicplayer.dao.user.userdaoimpl2.save(userdaoimpl2.java:22) @ adrian.example.musicplayer.service.user.servicesave.save(servicesave.java:19) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.aop.support.aoputils.invokejoinpointusingreflection(aoputils.java:317) @ org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:190) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:157) @ org.springframework.transaction.interceptor.transactioninterceptor$1.proceedwithinvocation(transactioninterceptor.java:98) @ org.springframework.transaction.interceptor.transactionaspectsupport.invokewithintransaction(transactionaspectsupport.java:262) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:95) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:179) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:207) @ com.sun.proxy.$proxy35.save(unknown source) @ adrian.example.musicplayer.web.controller.authorization.registrationcontroller.registrationpagepost(registrationcontroller.java:58) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:606) @ org.springframework.web.method.support.invocablehandlermethod.invoke(invocablehandlermethod.java:215) @ org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:132) @ org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:104) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlemethod(requestmappinghandleradapter.java:749) @ org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:689) @ org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:83) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:938) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:870) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:961) @ org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:863) @ javax.servlet.http.httpservlet.service(httpservlet.java:647) @ org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:837) @ 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.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:330) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:118) @ org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:84) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:113) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:103) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:113) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:154) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:45) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.authentication.www.basicauthenticationfilter.dofilter(basicauthenticationfilter.java:150) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.authentication.abstractauthenticationprocessingfilter.dofilter(abstractauthenticationprocessingfilter.java:199) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:110) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.csrf.csrffilter.dofilterinternal(csrffilter.java:105) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:50) @ org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:107) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:87) @ org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:342) @ org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:192) @ org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:160) @ org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:344) @ org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:261) @ 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:310) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ java.lang.thread.run(thread.java:745)
i solve problem.
what read custom validator object created hibernate , response save method.
https://stackoverflow.com/a/11493955/2870651
there have 2 option how solve this.
i found way
<bean id="validator" class="org.springframework.validation.beanvalidation.localvalidatorfactorybean" /> <property name="hibernateproperties"> <props> <prop key="javax.persistence.validation.factory">validator</prop> </props> </property>
Comments
Post a Comment