org.hibernate.QueryParameterException

时间:2014-09-10 13:19:54

标签: spring hibernate spring-mvc

我是春天的新手。我尝试通过spring和hibernate.delete对数据库执行添加,删除,编辑操作,并且添加操作已成功执行但当我尝试通过单击编辑进行编辑时。我收到此错误:

完整的堆栈跟踪:

org.hibernate.QueryParameterException: could not locate named parameter [UserId]
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterDescriptor(ParameterMetadata.java:75)
at org.hibernate.engine.query.ParameterMetadata.getNamedParameterExpectedType(ParameterMetadata.java:81)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:413)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:383)
at UserDao.userDaoImpl.getUserById(userDaoImpl.java:48)
at UserService.userServiceImpl.getUserById(userServiceImpl.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:108)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy17.getUserById(Unknown Source)
at UserController.userController.edit(userController.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

我的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>  

控制器类:&#34; userController.java&#34;

@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:&#34; userDaoImpl.java&#34;

@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 where 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;
}

}

1 个答案:

答案 0 :(得分:0)

需要更改如下

List<User> list = session.createQuery("from User u where u.id = :id").setParameter("id",id).list();