我想保存一篇文章,但我有一个例外。我不知道我做错了什么。 我有三个实体:User.class,Category.class和Article.class。
用户拥有以下属性:
类别具有以下属性:
文章有以下属性:
有我的控制器和JSP文件。
ArticleController.class
@Controller
public class ArticleController {
@Autowired
ArticleService articleService;
@Autowired
CategoryService categoryService;
@Autowired
UserService userService;
@ModelAttribute("article")
public Article construct(){
return new Article();
}
@RequestMapping("/admin/clanky")
public String articles(Model model){
model.addAttribute("articles", articleService.findAll());
model.addAttribute("categories", categoryService.findAll());
model.addAttribute("users", userService.findAll());
return "articles";
}
@RequestMapping(value="/admin/clanky", method=RequestMethod.POST)
public String saveArticle(@ModelAttribute("article") Article article, BindingResult result){
Date publishDate = new Date();
article.setPublishDate(publishDate);
articleService.save(article);
return "redirect:/admin/clanky.html?success=true";
}
@InitBinder
public void initBinder(WebDataBinder webDataBinder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
dateFormat.setLenient(false);
webDataBinder.registerCustomEditor(Date.class, "publishDate",
new CustomDateEditor(dateFormat, false));
}
@InitBinder
protected void initBinder(HttpServletRequest request,
ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Integer.class, new CustomNumberEditor(
Integer.class, true));
}}
article.jsp
<form:form commandName="article" cssClass="form-horizontal">
<div class="form-group">
<form:hidden path="id" class="form-control input-sm" />
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title:</label>
<div class="col-sm-10">
<form:input path="title" cssClass="form-control" />
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content:</label>
<div class="col-sm-10">
<form:textarea path="content" cssClass="form-control" rows="10" />
</div>
</div>
<div class="form-group">
<label for="publishDate" class="col-sm-2 control-label">Category:</label>
<div class="col-sm-10">
<form:select path="categories" cssClass="form-control">
<c:forEach items="${categories}" var="category">
<form:option class="form-control" value="${category.id}">${category.name}</form:option>
</c:forEach>
</form:select>
</div>
</div>
<div class="form-group">
<label for="user" class="col-sm-2 control-label">User:</label>
<div class="col-sm-10">
<form:select path="user.id" cssClass="form-control">
<c:forEach items="${users}" var="user">
<form:option class="form-control" value="${user.id}">${user.name}</form:option>
</c:forEach>
</form:select>
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<input type="submit" value="Uložit" class="btn btn-lg btn-primary">
</div>
</div>
有人可以帮助我吗?
有一个堆栈跟踪:
HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException
type Exception report
message Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException
org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1284)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
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.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:232)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
root cause
java.lang.NoClassDefFoundError: org/hibernate/QueryTimeoutException
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:644)
org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:799)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:669)
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.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy21.save(Unknown Source)
cz.michalsipek.blog.controller.ArticleController.saveArticle(ArticleController.java:55)
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.invokeHandlerMethod(HandlerMethodInvoker.java:175)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
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.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:232)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
root cause
java.lang.ClassNotFoundException: org.hibernate.QueryTimeoutException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:644)
org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:799)
org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:669)
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.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
com.sun.proxy.$Proxy21.save(Unknown Source)
cz.michalsipek.blog.controller.ArticleController.saveArticle(ArticleController.java:55)
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.invokeHandlerMethod(HandlerMethodInvoker.java:175)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
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.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:232)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
答案 0 :(得分:1)
在我看来,你正在使用Hibernate 3.x.如果是这种情况,我会确保您使用的是Hibernate核心版本3.6.10+。旧版本可能没有spring框架正在寻找的必要类定义。以下链接显示Hibernate 3.6.10具有您缺少的类定义:http://tool.oschina.net/uploads/apidocs/hibernate-3.6.10/org/hibernate/QueryTimeoutException.html
答案 1 :(得分:0)
保存de object时遇到同样的错误。好吧这很奇怪,但显然hibernate不理解0(零)作为@Id列的值。我试图保存一个引用的对象,其@Id值为0.所以我将id值更改为1并且它工作= S