Note that there are some explanatory texts on larger screens.

plurals
  1. POError with hibernate + Facelet JSF Managed Bean
    text
    copied!<p>I read <a href="http://netbeans.org/kb/docs/web/hibernate-webapp.html" rel="nofollow">this tutorial</a>. I try, but i get error. We have Mysql and netbean 7.0.1. A table with name : "customer", columns : "id int, name varchar, email varchar, description varchar". I used Hibernate to mapping table. This is my model class : </p> <pre><code> /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package model; import domain.Customer; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** * * @author xuanhung2401 */ public class CustomerModel { Session session = null; public CustomerModel(){ session = HibernateUtil.getSessionFactory().getCurrentSession(); } public List&lt;Customer&gt; getAllCustomer(int startId, int endId){ List&lt;Customer&gt; list = null; try{ session = HibernateUtil.getSessionFactory().getCurrentSession(); Transaction ts = session.beginTransaction(); Query query = session.createQuery("from Customer"); list = (List&lt;Customer&gt;)query.list(); } catch(Exception ex){ ex.printStackTrace(); } return list; } public Customer getById(int id){ Customer c = new Customer(); try{ Transaction ts = session.beginTransaction(); Query query = session.createQuery("from Customer as c where c.id = "+id ); c = (Customer)query.uniqueResult(); }catch(Exception ex){ ex.printStackTrace(); } return c; } public boolean updateCustomer(Customer c){ try{ Transaction ts = session.beginTransaction(); session.update(c); ts.commit(); return true; }catch(Exception ex){ ex.printStackTrace(); return false; } } public boolean addCustomer(Customer c){ try{ Transaction ts = session.beginTransaction(); session.save(c); ts.commit(); return true; }catch(Exception ex){ ex.printStackTrace(); return false; } } public boolean deleteCustomer(Customer c){ try{ Transaction ts = session.beginTransaction(); session.delete(c); ts.commit(); return true; }catch(Exception ex){ ex.printStackTrace(); return false; } } } </code></pre> <p>This is my controller : </p> <pre><code> /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package controller; import domain.Customer; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.faces.model.DataModel; import javax.faces.model.ListDataModel; import javax.faces.view.facelets.FaceletContext; import model.CustomerModel; /** * * @author xuanhung2401 */ @ManagedBean @SessionScoped public class CustomerController { CustomerModel model; DataModel customers; Customer currentCustomer; /** Creates a new instance of CustomerController */ public CustomerController() { model = new CustomerModel(); } public DataModel getCustomers(){ if (customers==null) { customers = new ListDataModel(model.getAllCustomer(1, 3)); } return customers; } public void recreateModel(){ customers = null; } public Customer getCurrentCustomer(){ if (currentCustomer==null) { currentCustomer = new Customer(); } return currentCustomer; } public String editCustomer(){ int id = 0; try { id = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")) ; currentCustomer = model.getById(id); if (currentCustomer!=null) { return "edit"; } }catch(Exception ex){ } return "myTemplateClient"; } public String editProcess(){ try{ model.updateCustomer(currentCustomer); recreateModel(); }catch(Exception ex){ } return "myTemplateClient"; } public String addCustomer(){ currentCustomer = new Customer(); return "add"; } public String addProcess(){ if (currentCustomer!=null) { model.addCustomer(currentCustomer); currentCustomer = new Customer(); recreateModel(); } return "myTemplateClient"; } public String deleteCustomer(){ int id = 0; id = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")) ; currentCustomer = model.getById(id); model.deleteCustomer(currentCustomer); recreateModel(); return "myTemplateClient"; } public String goIndex(){ return "myTemplateClient"; } public String prepareView(){ currentCustomer = (Customer) customers.getRowData(); if (currentCustomer !=null) { return "details"; } return "myTemplateClient"; } } </code></pre> <p>As you see, we have 4 view : myTemplateClient.xhtml, add.xhtml, edit.xhtml, details.xhtml. i navigate by " return "viewname"; " command. Problem are :</p> <ol> <li><p>Address bar is not the same address with page i am viewing. Example : i'm reading myTemplateClient.xhtml, but address bar is : localhost:8080/iDo_Hibernate/faces/details.xhtml ( it must be : localhost:8080/iDo_Hibernate/faces/myTemplateClient.xhtml). After that, when i jump to add.xhtml, address bar is : localhost:8080/iDo_Hibernate/faces/myTemplateClient.xhtml.</p></li> <li><p>After i add new customer, it's redirect to "myTemplateClient" page ( this is index page, it shows all customer) with address bar is : localhost:8080/iDo_Hibernate/faces/add.xhtml. Now, when i refresh my browser, it adds more customer with the same information. I try to clear added object, but still error.</p> <p>Please help me fix that errors (Forgive me because my English is not good). Thanks for reading.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload