spring - org.hibernate.QueryParameterException -
i newbie in spring. i'm trying perform add,delete,edit operation on database through spring , hibernate.delete , add operation executed when trying edit clicking on edit. i'm getting error:
full stacktrace:
org.hibernate.queryparameterexception: not locate named parameter [userid] @ org.hibernate.engine.query.parametermetadata.getnamedparameterdescriptor(parametermetadata.java:75) @ org.hibernate.engine.query.parametermetadata.getnamedparameterexpectedtype(parametermetadata.java:81) @ org.hibernate.impl.abstractqueryimpl.determinetype(abstractqueryimpl.java:413) @ org.hibernate.impl.abstractqueryimpl.setparameter(abstractqueryimpl.java:383) @ userdao.userdaoimpl.getuserbyid(userdaoimpl.java:48) @ userservice.userserviceimpl.getuserbyid(userserviceimpl.java:41) @ 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:307) @ org.springframework.aop.framework.reflectivemethodinvocation.invokejoinpoint(reflectivemethodinvocation.java:183) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:150) @ org.springframework.transaction.interceptor.transactioninterceptor.invoke(transactioninterceptor.java:108) @ org.springframework.aop.framework.reflectivemethodinvocation.proceed(reflectivemethodinvocation.java:172) @ org.springframework.aop.framework.jdkdynamicaopproxy.invoke(jdkdynamicaopproxy.java:202) @ com.sun.proxy.$proxy17.getuserbyid(unknown source) @ usercontroller.usercontroller.edit(usercontroller.java:51) @ 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.bind.annotation.support.handlermethodinvoker.doinvokemethod(handlermethodinvoker.java:710) @ org.springframework.web.bind.annotation.support.handlermethodinvoker.invokehandlermethod(handlermethodinvoker.java:167) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.invokehandlermethod(annotationmethodhandleradapter.java:414) @ org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter.handle(annotationmethodhandleradapter.java:402) @ org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:771) @ org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:716) @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:647) @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:552) @ javax.servlet.http.httpservlet.service(httpservlet.java:620) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:303) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:52) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:241) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:208) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:220) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:122) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:501) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:950) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:116) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1040) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:607) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:316) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615) @ org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) @ java.lang.thread.run(thread.java:745)
my jsp: "user.jsp"
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>spring</title> </head> <body> <h2>add user data</h2> <form:form method="post" modelattribute="user" action="/springhibernate1/adduser"> <table> <tr> <td><form:label path="name">name:</form:label></td> <td><form:input path="name" value="${user.name}"/></td> </tr> <tr> <td><form:label path="department">department:</form:label></td> <td><form:input path="department" value="${user.department}"/></td> </tr> <tr> <td><form:label path="password">password:</form:label></td> <td><form:input path="password" value="${user.password}"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="submit"/></td> </tr> </table> </form:form> <h2>list users</h2> <table align="left" border="1"> <tr> <th>id</th> <th>name</th> <th>department</th> <th>password</th> <th>actions on row</th> </tr> <c:foreach items="${userlist}" var="user1"> <tr> <td><c:out value="${user1.id}"/></td> <td><c:out value="${user1.name}"/></td> <td><c:out value="${user1.department}"/></td> <td><c:out value="${user1.password}"/></td> <td align="center"><a href="edit/${user1.id}">edit</a> | <a href="delete/${user1.id}">delete</a></td> </tr> </c:foreach> </table> </body> </html>
controller class:" usercontroller.java"
@controller public class usercontroller { @autowired private userservice userservice; @requestmapping("/user") public string listuser(map<string, object> map) { map.put("user", new user()); map.put("userlist", userservice.listuser()); return "user"; } @requestmapping(value = "/adduser",method = requestmethod.post) public string adduser(@modelattribute("user")user user, bindingresult result) { if(null == user.getid()) { userservice.adduser(user); } else { userservice.updateuser(user); } return "redirect:/user"; } @requestmapping("/delete/{userid}") public string deleteuser(@pathvariable("userid") integer userid) { userservice.deleteuser(userid); return "redirect:/user"; } @requestmapping("/edit/{userid}") public string edit(@pathvariable("userid")integer userid,map<string, object> map) { map.put("user", userservice.getuserbyid(userid)); map.put("userlist", userservice.listuser()); return "user"; } }
dao implemetation: "userdaoimpl.java"
@repository public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; @override public void adduser(user user) { sessionfactory.getcurrentsession().save(user); } @override public void deleteuser(integer id) { user user = (user)sessionfactory.getcurrentsession().load(user.class,id); if(null != user) { sessionfactory.getcurrentsession().delete(user); } } @override @suppresswarnings("unchecked") public list<user> listuser() { return sessionfactory.getcurrentsession().createquery("from user").list(); } @override public void updateuser(user user) { sessionfactory.getcurrentsession().update(user); } @override @suppresswarnings("unchecked") public user getuserbyid(integer id) { session session = sessionfactory.getcurrentsession(); list<user> list = session.createquery("from user u u.id = :id").setparameter("userid",id).list(); return list.size() > 0 ?(user)list.get(0): null; } public sessionfactory getsessionfactory() { return sessionfactory; } public void setsessionfactory(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } }
need change below
list<user> list = session.createquery("from user u u.id = :id").setparameter("id",id).list();
Comments
Post a Comment