如果通过REST调用Hibernate DAO get方法不起作用,否则

时间:2015-07-12 21:13:58

标签: java spring hibernate spring-mvc dao

我想知道是否有人可以在DAOSpring网络应用中Hibernate解释为什么以下代码:

@Override
public Book get(Integer bookId) {
    logger.info(BookDAOImpl.class.getName() + ".get() method called.");

    Session session = sessionFactory.openSession();          
    try {    
        System.out.println("Book : " + bookId);
        Book book = (Book) session.get(Book.class, bookId);    
        return book;
    }        
    finally {
        session.close();
    }                 
}    

在其父网络应用程序中可以很好地工作,但是通过controllerservice类通过REST调用时:

// Gets a Book via REST.
@RequestMapping(value="/book/{bookId}", method=RequestMethod.GET)
public @ResponseBody Object getBook(@PathVariable("bookId") String bookId) {          
    logger.info(RestController.class.getName() + ".getBook() method called.");                 
    if (bookIdValid(bookId)) {            
        Book book = bookService.get(Integer.parseInt(bookId)); 
        System.out.println("ABook " + book.toString());
        return book;             
    }
    else {             
        Messages messages = new Messages();
        messages.addMessage(messageSource.getMessage("bookId.required", null, null));                                    
        return messages;            
    }        
}  

Book book = (Book) session.get(Book.class, bookId);返回null。

BookDAOImpl的第53行是以下方法中的行return bookId == book.getBookId().intValue();

@Override
public boolean check(Integer bookId) {
    logger.info(BookDAOImpl.class.getName() + ".check() method called.");

    try {
        Book book = this.get(bookId);
        return bookId == book.getBookId().intValue();
    }
    catch(IndexOutOfBoundsException e) {
        return false;
    }
}    

堆栈跟踪是:

    Book : 78
INFO|12 07 2015|22 07 16|http-apr-8080-exec-123|library.controller.PersonController| - library.controller.ExceptionController.exception() method called.
null
library.dao.BookDAOImpl.check(BookDAOImpl.java:53)
library.service.BookServiceImpl.check(BookServiceImpl.java:32)
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:483)
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
com.sun.proxy.$Proxy571.check(Unknown Source)
library.controller.RestController.bookIdValid(RestController.java:127)
library.controller.RestController.getBook(RestController.java:102)
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:483)
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:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
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.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2451)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2440)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:744)
library.interceptor.ClientInterceptor.afterCompletion() method completed: GET: /Library/rest/book/78?null at 12 07 2015 @ 22 07 ended with HTTP code: 500.

1 个答案:

答案 0 :(得分:0)

看起来// follow mouse var $mouseX = 0, $mouseY = 0; var $xp = 0, $yp =0; $(document).mousemove(function(e){ $mouseX = e.pageX; $mouseY = e.pageY - $(window).scrollTop(); }); var $loop = setInterval(function(){ var scrolltop = $(this).scrollTop(); var pos; $xp += (($mouseX - $xp)/50); $yp += (($mouseY - $yp)/50); var deg = 0; var hozDiff = Math.floor($mouseX - $xp); var maxAngle = 45; if (hozDiff > 0){ $("#kirby").attr('src', 'http://media.giphy.com/media/6Uj6lFq4KWDQY/giphy.gif'); if (hozDiff < maxAngle) deg = hozDiff; else deg = maxAngle; } else { $("#kirby").attr('src', 'http://media.giphy.com/media/6Uj6lFq4KWDQY/giphy.gif'); if (hozDiff > -maxAngle) deg = hozDiff; else deg = -maxAngle; } $("#kirby").css({left:$xp +'px', top:$yp +'px', position:'fixed', 'transform': 'rotate(' + (deg++) + 'deg)', '-moz-transform': 'rotate(' + (deg++) + 'deg)', '-o-transform': 'rotate(' + (deg++) + 'deg)', '-webkit-transform': 'rotate(' + (deg++) + 'deg)' }); }, 30);返回null - 您的数据库可能不包含第78页。您是否有可用于在命中get端点之前插入数据库的create book端点?