0

I have created an application using Spring MVC 3, Hibernate and Ext Js 4. The problem is that when I start the application the data is not readed from the database.

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

The error is raised from the controller: Error retrieving books from database. Do you have any ideea what can be the problem? See here the Console output: http://pastebin.com/jMQKS31P

FIXED!!!

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

Community
  • 1
  • 1
Pascut
  • 3,291
  • 6
  • 36
  • 64
  • General exception handlers are no-no(I think *now* you know why :D) Could you at least call e.printStackTrace() and add what it prints(edit the question). – Boris Treukhov Jan 21 '13 at 19:16
  • found it: org.springframework.orm.hibernate3.HibernateQueryException: books is not mapped [SELECT COUNT(*) FROM books]; nested exception is org.hibernate.hql.ast.QuerySyntaxException: books is not mapped [SELECT COUNT(*) FROM books] :D, noy i'll try to fix it.. – Pascut Jan 21 '13 at 19:23
  • You can find good answers here: http://stackoverflow.com/a/14447201/1564840 – Pascut Jan 21 '13 at 21:08

1 Answers1

1

You're passing a SQL request, using tables and column names, to a method which expects an HQL request, using entities, mapped fields and associations. SQL and HQL are two different query languages.

The HQL query should be

select count(book.id) from Book book

If you don't know about HQL, then you really need to read the documentation. Using Hibernate without knowing HQL is like using JDBC without knowing SQL.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Caused by: org.hibernate.hql.ast.QuerySyntaxException: Book is not mapped [select count(book.id) from Book book] – Pascut Jan 21 '13 at 20:51
  • Then you forgot to add the Book entity to the hibernate configuration file. Hibernate doesn't magically discovers your entities. You have to list them in the config file. – JB Nizet Jan 21 '13 at 20:53