现在还有个缺陷就是每次都要new一个BillDAO来执行,而且在BillDAO中每次都要生成一个emf,然后又emf.close()。
明天要完善一下!

BillBB:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.pz.backing;

import com.pz.dao.BillDAO;
import com.pz.entity.BillEO;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.event.ActionEvent;

/**
*
* @author bache
*/

public class BillBB {

         private List<BillEO> billEOs;

         public void act_Add(ActionEvent e) {
                UIComponent comp = e.getComponent().getParent();
                 int id = Integer.parseInt(((HtmlInputText) comp.findComponent( "id")).getValue().toString());
                BillEO billEO = new BillEO();
                billEO.setId(id);
                add(billEO);
        }

         public List<BillEO> getBillEOs() {
                flushList();
                 return billEOs;
        }

         public void setBillEOs(List<BillEO> billEOs) {
                 this.billEOs = billEOs;
                reset();
        }

         private void add(BillEO billEOs) {
                 new BillDAO().add(billEOs);
                reset();
        }

         private void flushList() {
                 if(billEOs == null){
                        billEOs = new BillDAO().findAll();
                }
        }
         private void reset(){
                billEOs = null;
        }
}

BillDAO:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


package com.pz.dao;

import com.pz.entity.BillEO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

/**
*
* @author bache
*/

public class BillDAO {
        List<BillEO> billEOs;
         private EntityManager em;
        @PersistenceUnit(unitName= "JPAtestPU")
         private EntityManagerFactory emf;

         public void add(BillEO billEOs) {
                em = getEntityManager();
                em.getTransaction().begin();
                em.persist(billEOs);
                em.getTransaction().commit();
                em.close();emf.close();
        }
        
         private EntityManager getEntityManager(){
                 if( null == emf){
                        emf = Persistence.createEntityManagerFactory( "JPAtestPU");
                }
                 return emf.createEntityManager();
        }
        
         public List<BillEO> findAll(){
                em = getEntityManager();
                Query query = em.createQuery( "SELECT b FROM BillEO b");
                billEOs = query.getResultList();
                em.close();
                emf.close();
                 return billEOs;
        }
        
}