javahibernatejsfdatatablepropertynotfoundexception

JSF and Hibernate Issue in finding a specific record from database and displaying all data


I have written some codes which can perform CRUD functions in database using JSF, Hibernate and MySQL. The only issue is happening that when I implement the find and getAllRecords, the application does not work. Some my codes are below:

BookController.java Class

public class BookController {
private int isbn;
private String title;

public int getIsbn() {
    return isbn;
}
public void setIsbn(int isbn) {
    this.isbn = isbn;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}

public String findBook() throws Exception{
    BookManager bm = new BookManager();
    bm.findBook(isbn);

    return "find";
}

public String getAllBook() throws Exception{
    BookManager bm = new BookManager();
    bm.getAllBooks();

    return "all";
}
}

BookManager.java Class

public class BookManager {

private SessionFactory sessionFactory;

public BookManager() throws Exception{

    //A sessionFactory is set up once for an application

    sessionFactory = new Configuration()
            .configure().buildSessionFactory(); // configures settings from hibernate.cfg.xml

}


public int addBook(int isbn, String title){
    Session session = sessionFactory.openSession();
    Transaction tx = session.beginTransaction();
    Book b = new Book();
    b.setIsbn(isbn);
    b.setTitle(title);
    int BookID = (Integer)session.save(b);
    tx.commit();
session.close();
return BookID;
}


public void updateBook(int isbn, String title){
    Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
            Book b = new Book();
                b.setIsbn(isbn);
                b.setTitle(title);
            session.update(b);
        tx.commit();
    session.close();    
}

public Book findBook(int isbn){
    Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
            Book b = (Book)session.get(Book.class, isbn);
        tx.commit();
    session.close();
    return b;
}

public void deleteBook(int isbn){
    Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
            Book b = (Book)session.get(Book.class, isbn);
            session.delete(b);
        tx.commit();
    session.close();    
}

@SuppressWarnings("unchecked")
public ArrayList<Book> getAllBooks(){
    Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
            ArrayList<Book> allBooks = (ArrayList<Book>)session.createQuery("from Book").list();
        tx.commit();
    session.close();
return allBooks;    
}
 }

Find.jsp

<h:form>

    ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>

    <h:commandButton action="#{bookController.findBook}" value="find" /> <br>

    Book Found is <h:outputLabel value="#{bookController.title }" />

</h:form>

getAllBooks.jsp

  <h:form>
    <h:dataTable value = "#{bookController.allBook }" var = "all1">
    <f:facet name = "header">
    <h:outputText value="Existing Books" />
    </f:facet>

    <h:column>
        <f:facet name = "header">
            <f:verbatim>ISBN </f:verbatim>
        </f:facet>
        <h:outputText value="#{all1.book_isbn }" /> <!--  book_isbn column in table db -->
    </h:column>

        <h:column>
        <f:facet name = "header">
            <f:verbatim>Title</f:verbatim>
        </f:facet>
        <h:outputText value="#{all1.book_title }" />  <!--  book_title column in table db -->
    </h:column> 
    </h:dataTable>  
  </h:form>

The error for getAllBooks I get is

javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'book_isbn'.

The error for findBook I get is

javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation


Solution

  • You are returning a String in the getAllBook() , so you should return an ArrayList<Book> (getAllBook() of the BookController.java class )

    public ArrayList<Book> getAllBook() throws Exception{
        BookManager bm = new BookManager();
        return bm.getAllBooks();
    }
    

    and for the find page, same thing , in the BookController.java your method shouldn't return a String , you have to return a Book object:

    public void findBook() throws Exception{
        BookManager bm = new BookManager();
        Book book = bm.findBook(isbn);
        this.title = book.getTitle();
    }
    

    .

    add the update attribute to your comandButton .

    <h:form>
    
        ISBN: <h:inputText value = "#{bookController.isbn} "/> <br>
    
        <h:commandButton action="#{bookController.findBook}" value="find" update="@form" /> <br>
    
        Book Found is <h:outputLabel value="#{bookController.title }" />
    
    </h:form>