Java 从数据库读取数据时发生休眠错误

Java 从数据库读取数据时发生休眠错误,java,eclipse,spring,hibernate,extjs4,Java,Eclipse,Spring,Hibernate,Extjs4,我使用SpringMVC3、Hibernate和ExtJS4创建了一个应用程序。问题是,当我启动应用程序时,数据没有从数据库中读取 BookController.java: @Controller public class BookController { private BookService bookService; @RequestMapping(value="/books/view.action") public @ResponseBody Map<String,? exte

我使用SpringMVC3、Hibernate和ExtJS4创建了一个应用程序。问题是,当我启动应用程序时,数据没有从数据库中读取

BookController.java:

@Controller
public class BookController  {

private BookService bookService;

@RequestMapping(value="/books/view.action")
public @ResponseBody Map<String,? extends Object> view(@RequestParam int start, @RequestParam int limit) throws Exception {

    try{

        List<Book> books = bookService.getBookList(start,limit);

        int total = bookService.getTotalBooks();

        return ExtJSReturn.mapOK(books, total);

    } catch (Exception e) {

        return ExtJSReturn.mapError("Error retrieving books from database.");
    }
}
ExtJsReturn.java:

@Component
public class ExtJSReturn {

/**
 * Generates modelMap to return in the modelAndView
 * @param books
 * @return
 */
public static Map<String,Object> mapOK(List<Book> books){

    Map<String,Object> modelMap = new HashMap<String,Object>(3);
    modelMap.put("total", books.size());
    modelMap.put("data", books);
    modelMap.put("success", true);

    return modelMap;
}

/**
 * Generates modelMap to return in the modelAndView
 * @param books
 * @return
 */
public static Map<String,Object> mapOK(List<Book> books, int total){

    Map<String,Object> modelMap = new HashMap<String,Object>(3);
    modelMap.put("total", total);
    modelMap.put("data", books);
    modelMap.put("success", true);

    return modelMap;
}

/**
 * Generates modelMap to return in the modelAndView in case
 * of exception
 * @param msg message
 * @return
 */
public static Map<String,Object> mapError(String msg){

    Map<String,Object> modelMap = new HashMap<String,Object>(2);
    modelMap.put("message", msg);
    modelMap.put("success", false);

    return modelMap;
} 
}
从控制器引发错误:从数据库检索书籍时出错。 你知道会有什么问题吗? 请参见此处的控制台输出:

修正


您正在使用表和列名将SQL请求传递给一个方法,该方法使用实体、映射字段和关联预期HQL请求。SQL和HQL是两种不同的查询语言

HQL查询应该是

select count(book.id) from Book book

如果您不了解HQL,那么您真的需要阅读。在不知道HQL的情况下使用Hibernate就像在不知道SQL的情况下使用JDBC。

一般异常处理程序不是noI我想现在你知道为什么了:D你能至少调用e.printStackTrace并添加它打印的内容吗?问题是:org.springframework.orm.hibernate3.HibernateQueryException:图书未映射[从图书中选择计数];嵌套异常为org.hibernate.hql.ast.QuerySyntaxException:books未映射[SELECT COUNT FROM books]:D,不,我将尝试修复它..您可以在这里找到好的答案:原因:org.hibernate.hql.ast.QuerySyntaxException:Book未映射[SELECT countbook.id FROM Book]然后您忘了将Book实体添加到hibernate配置文件中。Hibernate不会神奇地发现你的实体。您必须在配置文件中列出它们。
@JsonAutoDetect
@Entity
@Table(name="books")
public class Book {

@Id
@GeneratedValue
@Column(name="id")
private int id;

@Column(name="title", nullable=false)
private String title;

@Column(name="author", nullable=false)
private String author;

@Column(name="publisher", nullable=false)
private String publisher;

@Column(name="isbn", nullable=false)
private String isbn;

@Column(name="pages", nullable=false)
private int pages;

@Column(name="category", nullable=false)
private String category;

@Column(name="qty", nullable=false)
private int qty;

/**
 * @return the title
 */
public String getTitle() {
    return title;
}

/**
 * @param title the title to set
 */
public void setTitle(String title) {
    this.title = title;
}

/**
 * @return the author
 */
public String getAuthor() {
    return author;
}

/**
 * @param author the author to set
 */
public void setAuthor(String author) {
    this.author = author;
}

/**
 * @return the publisher
 */
public String getPublisher() {
    return publisher;
}

/**
 * @param publisher the publisher to set
 */
public void setPublisher(String publisher) {
    this.publisher = publisher;
}

/**
 * @return the isbn
 */
public String getIsbn() {
    return isbn;
}

/**
 * @param isbn the isbn to set
 */
public void setIsbn(String isbn) {
    this.isbn = isbn;
}

/**
 * @return the pages
 */
public int getPages() {
    return pages;
}

/**
 * @param pages the pages to set
 */
public void setPages(int pages) {
    this.pages = pages;
}

/**
 * @return the category
 */
public String getCategory() {
    return category;
}

/**
 * @param category the category to set
 */
public void setCategory(String category) {
    this.category = category;
}

/**
 * @return the qty
 */
public int getQty() {
    return qty;
}

/**
 * @param qty the qty to set
 */
public void setQty(int qty) {
    this.qty = qty;
}

/**
 * @return the id
 */
public int getId() {
    return id;
}

/**
 * @param id the id to set
 */
public void setId(int id) {
    this.id = id;
}


}
@Component
public class ExtJSReturn {

/**
 * Generates modelMap to return in the modelAndView
 * @param books
 * @return
 */
public static Map<String,Object> mapOK(List<Book> books){

    Map<String,Object> modelMap = new HashMap<String,Object>(3);
    modelMap.put("total", books.size());
    modelMap.put("data", books);
    modelMap.put("success", true);

    return modelMap;
}

/**
 * Generates modelMap to return in the modelAndView
 * @param books
 * @return
 */
public static Map<String,Object> mapOK(List<Book> books, int total){

    Map<String,Object> modelMap = new HashMap<String,Object>(3);
    modelMap.put("total", total);
    modelMap.put("data", books);
    modelMap.put("success", true);

    return modelMap;
}

/**
 * Generates modelMap to return in the modelAndView in case
 * of exception
 * @param msg message
 * @return
 */
public static Map<String,Object> mapError(String msg){

    Map<String,Object> modelMap = new HashMap<String,Object>(2);
    modelMap.put("message", msg);
    modelMap.put("success", false);

    return modelMap;
} 
}
select count(book.id) from Book book