2013-01-21 80 views
0

我創建了一個使用Spring MVC 3,Hibernate和Ext Js 4的應用程序。問題是,當我啓動應用程序時,數據不會從數據庫中讀取。從數據庫中讀取數據的休眠錯誤

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."); 
    } 
} 

BookService.java:

@Service 
public class BookService { 

private BookDAO bookDAO; 

/** 
* Get all books 
* @return 
*/ 
@Transactional(readOnly=true) 
public List<Book> getBookList(int start, int limit){ 

    return bookDAO.getBooks(start, limit); 
} 

    public int getTotalBooks(){ 

    return bookDAO.getTotalBooks(); 
} 

BookDAO.java:

@SuppressWarnings("unchecked") 
public List<Book> getBooks(int start, int limit) { 

    DetachedCriteria criteria = DetachedCriteria.forClass(Book.class); 

    return hibernateTemplate.findByCriteria(criteria, start, limit); 
} 

    public int getTotalBooks(){ 
    return DataAccessUtils.intResult(hibernateTemplate.find("SELECT COUNT(*) FROM books")); 
} 

Book.java:

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


} 

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

錯誤從控制器提出:從數據庫中檢索錯誤的書籍。 你有什麼可以成爲問題的ideea? 在這裏看到控制檯輸出:http://pastebin.com/jMQKS31P

固定!!!

https://stackoverflow.com/a/14447201/1564840

+0

一般異常處理程序是否否(我認爲*現在*你知道爲什麼:D)你至少可以調用e.printStackTrace()並添加它打印的內容(編輯問題)。 –

+0

找到它:org.springframework.orm.hibernate3.HibernateQueryException:書未映射[SELECT COUNT(*)FROM books];嵌套的異常是org.hibernate.hql.ast.QuerySyntaxException:書籍沒有被映射[SELECT COUNT(*)FROM books]:D,noy我會嘗試修復它.. – Pascut

+0

你可以在這裏找到很好的答案:http:/ /stackoverflow.com/a/14447201/1564840 – Pascut

回答

1

你傳遞一個SQL請求,使用表和列名,到預計的HQL要求,使用單位,映射的字段和關聯的方法。 SQL和HQL是兩種不同的查詢語言。

HQL查詢應該是

select count(book.id) from Book book 

如果你不知道HQL,那麼你真的需要閱讀the documentation。在不知道HQL的情況下使用Hibernate就像在不知道SQL的情況下使用JDBC。

+0

引起:org.hibernate.hql.ast.QuerySyntaxException:圖書未映射[從書本中選擇count(book.id)] – Pascut

+0

然後,您忘記將Book實體添加到休眠配置文件。 Hibernate不會奇蹟般地發現你的實體。你必須在配置文件中列出它們。 –

+0

現在它正在工作,就像你說的,謝謝! – Pascut