Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6374283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:28:07+00:00 2026-05-25T01:28:07+00:00

I read this tutorial . I try, but i get error. We have Mysql

  • 0

I read this tutorial. 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 :

 /*
 * 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<Customer> getAllCustomer(int startId, int endId){
        List<Customer> list = null;
        try{
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction ts = session.beginTransaction();
            Query query = session.createQuery("from Customer");
            list = (List<Customer>)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;
        }
    }
}

This is my controller :

   /*
 * 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";
    }
    
   
}

As you see, we have 4 view : myTemplateClient.xhtml, add.xhtml, edit.xhtml, details.xhtml. i navigate by " return "viewname"; " command. Problem are :

  1. 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.

  2. 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.

    Please help me fix that errors (Forgive me because my English is not good). Thanks for reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:28:08+00:00Added an answer on May 25, 2026 at 1:28 am

    It seems that the issue with urls is because you are using forward to navigate between pages. Use sendRedirect instead. More information here: http://www.javapractices.com/topic/TopicAction.do?Id=181

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this PHP RegEx page , but either I'm missing something, misreading something,
I read this article Managing Hierarchical Data in MySQL At the end of this
I am using tutorial (now inaccessible) UITableView - Searching table view. but I really
I've read several example programs and tutorials to try and solve my problem but
I have application based on this tutorial Method I use to test connection to
I have read several tutorials but I still do not have any clue :-)
I (like many others) followed the webview tutorial, but I can't get pages to
I read this answer and its comments and I'm curious: Are there any reasons
I read this post last night, and I noticed it was from 2006. I
I read This article and i found it interesting. To sum it up for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.