springMVC集合和hibrnate 3.3.2 GA java.lang.Integer无法强制转换为java.lang.Long

时间:2013-02-26 03:03:31

标签: spring hibernate casting

Iam在cast上犯了一个错误的错误。我不知道如何修复它。请...帮我摆脱this.springMVC集合和hibrnate 3.3.2 GA java.lang.Integer不能被强制转换to java.lang.Long这个异常没有给出任何错误...仍然没有运行...........不知道..........

HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


root cause 

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
    com.os.springjpa.controller.BookController.listPaging(BookController.java:103)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.20

## BookController.java ##

package com.os.springjpa.controller;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.os.springjpa.dao.BookDao;
import com.os.springjpa.entity.Book;
import com.os.springjpa.utils.Utils;

/**
 * @author kris;
 *         This class is using Path Variable style as example like Rest
 *         Style. Path Variable will handling like url string but so simple then
 *         url string. I'm also still not found how to make url string as simple
 *         as posible in view layer.
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookDao bookDao;
    private final int PAGE_SIZE = 2;
    Logger logger = Logger.getLogger(BookController.class);

    @RequestMapping(method = RequestMethod.GET)
    public String form(Model model) {
    System.out.println(org.hibernate.Version.getVersionString());
        model.addAttribute(new Book());
        return "book/create";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Book book, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            logger.info("Error has been detect");
            return "book/create";
        }
        if (bookDao.saveOrUpdate(book)) {
            book = bookDao.getByIsbn(book.getIsbn());
            return "redirect:/book/" + book.getId();
        } else {
            return "/exception";
        }
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String update(@Valid Book book, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            logger.info("Error has been detect");
            return "book/update";
        }
        if (bookDao.saveOrUpdate(book)) {
            return "redirect:/book/" + book.getId();
        } else {
            return "/exception";
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable Integer id) {
        logger.info("Delete Method Has Call");
        Book book = bookDao.getById(id);
        if (bookDao.delete(book)) {
            return "redirect:/book/list/1";
        } else {
            return "/exception";
        }
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String detail(@PathVariable Integer id, Model model) {
        Book book = bookDao.getById(id);
        if (book == null) {
            return "/exception";
        }
        model.addAttribute(book);
        return "book/detail";
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, Model model) {
        Book book = bookDao.getById(id);
        if (book == null) {
            return "/exception";
        }
        model.addAttribute(book);
        return "book/update";
    }

    @RequestMapping(value = "/list/{page}", method = RequestMethod.GET)
    public String listPaging(@PathVariable Integer page, Model model) {
        // Create simple pagination
        model.addAttribute("books", bookDao.getAll((page - 1) * PAGE_SIZE, PAGE_SIZE));
       model.addAttribute("pageCount", Utils.getPageCount(PAGE_SIZE, (Long)bookDao.countAllBook().get(0)));
        return "book/list";
    }

}

Utils.java


package com.os.springjpa.utils;

public class Utils {

    public static int getPageCount(int pageSize, Long row) {
        int result = 0;
        if (row % pageSize == 0) {
            result = (int) (row / pageSize);
        } else {
            result = (int) (row / pageSize + 1);
        }
        return result;
    }
}

请帮助我......

2 个答案:

答案 0 :(得分:1)

在BookController.java第103行

 model.addAttribute("pageCount", Utils.getPageCount(PAGE_SIZE, 
                                 new Long(bookDao.countAllBook().get(0))));

答案 1 :(得分:1)

试试这个,

model.addAttribute("pageCount", Utils.getPageCount(PAGE_SIZE, Long.parseLong(bookDao.countAllBook().get(0).toString())));