取消在相关实体之间的JPA更新传播

取消在相关实体之间的JPA更新传播,jpa,persistence,glassfish-3,Jpa,Persistence,Glassfish 3,更新:我已经考虑了Chris的答案,但没有帮助-我仍然面临这个问题。我已经更新了下面的代码以合并Chris的答案。需要注意的是,在实现Chris的建议时,这些关系一直存在,但没有反映在view.xhtml页面上。我必须用调用GenericDao.update()返回的对象替换db对象 我有以下关系: 一个客户对多个采购订单(PO) 一张订单对应多张发票 我已经读过双向关系,我知道如果我有双向关系,我应该在更新实体时更新关系的两侧 我执行以下步骤: 创建客户(客户1) 创建链接到客户1的采购订单(

更新:我已经考虑了Chris的答案,但没有帮助-我仍然面临这个问题。我已经更新了下面的代码以合并Chris的答案。需要注意的是,在实现Chris的建议时,这些关系一直存在,但没有反映在view.xhtml页面上。我必须用调用
GenericDao.update()
返回的对象替换db对象

我有以下关系:

一个客户对多个采购订单(PO) 一张订单对应多张发票

我已经读过双向关系,我知道如果我有双向关系,我应该在更新实体时更新关系的两侧

我执行以下步骤:

  • 创建客户(客户1)
  • 创建链接到客户1的采购订单(订单1)
  • 创建链接到订单1的发票(发票1)
  • 我所观察到的是,所有实体和关系都被持久化,但客户订单列表不显示

    查看客户:

    查看采购订单:

    数据库查询:

    > select * from customer; > +----+------------+ > | ID | NAME | > +----+------------+ > | 1 | Customer 1 | > +----+------------+ > 1 row in set (0.00 sec) > > mysql> select * from purchaseorder; > +----+---------+-------------+ > | ID | NAME | customer_id | > +----+---------+-------------+ > | 1 | Order 1 | 1 | > +----+---------+-------------+ > 1 row in set (0.00 sec) > > mysql> select * from invoice; > +----+-----------+------------------+ > | ID | NAME | purchaseorder_id | > +----+-----------+------------------+ > | 1 | Invoice 1 | 1 | > +----+-----------+------------------+ > 1 row in set (0.00 sec) GenericDao(所有其他DAO的父级)

    遥控器

    @RequestScoped
    public class CustomerController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private ICustomerDao customerDao;
        @Inject
        private Customer customer;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == this.getCustomer())
            {
                setCustomer(new Customer());
            }
        }
    
        public void processRequest(FormActionToPerform action) throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    setCustomer(new Customer());
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    setCustomer((Customer) getHtmlDataTableActionBean()
                            .getSelectedEntityObject());
                    break;
                case SHOW_DELETE_VIEW:
                    setCustomer((Customer) getHtmlDataTableActionBean()
                            .getSelectedEntityObject());
                    delete();
                    break;
            }
        }
    
        public String doShowUIView(FormActionToPerform action)
        {
            String responseURL = "fail.xhtml";
            if (null == this.customer)
            {
                return responseURL;
            }
            else
            {
                switch (action)
                {
                    case SHOW_ADD_VIEW:
                        responseURL = "customer.xhtml";
                        break;
                    case SHOW_EDIT_VIEW:
                        responseURL = "customer.xhtml";
                        break;
                    case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                        responseURL = "viewCustomer.xhtml";
                        break;
                    case SHOW_DELETE_VIEW:
                        responseURL = "customerList.xhtml";
                        break;
                    case SHOW_VIEW_FOR_LIST:
                        if (this.entityObjectList.size() == 0)
                        {
                            setErrorMessage("No customers to display");
                        }
                        responseURL = "customerList.xhtml";
                        break;
                    default:
                        responseURL = "index.xhtml";
                }
            }
            return responseURL;
        }
    
        public String save()
        {
            String url = "success.xhtml";
            Customer existingCustomer = null;
            try
            {
                existingCustomer =
                        customerDao.getCustomerByName(this.getCustomer().getName());
    
                if(existingCustomer != null)
                {
                    //there's already a customer with this name, don't make a new one
                    setErrorMessage("Customer already exists");
                    url = "fail.xhtml";
                }
    
                customerDao.update(customer);
            }
            catch (DatabaseException e)
            {
                setErrorMessage(e.toString());
                e.printStackTrace();
                url = "fail.xhtml";
            }
            return url;
        }
    }
    
    @RequestScoped
    public class PurchaseOrderController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private IPurchaseOrderDao purchaseOrderDao;
        @EJB
        private IInvoiceDao invoiceDao;
        @EJB
        private ICustomerDao customerDao;
        @Inject
        private PurchaseOrder purchaseOrder;
    
        private List<SelectItem> customerList;
        private String selectedCustomer;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == purchaseOrder)
            {
                purchaseOrder = new PurchaseOrder();
                setEditMode(false);
            }
        }
    
        public void processRequest(FormActionToPerform action)
        throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    setPurchaseOrder(new PurchaseOrder());
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    {
                        setPurchaseOrder(
                              (PurchaseOrder)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    }
                    break;
                case SHOW_DELETE_VIEW:
                    {
                        setPurchaseOrder(
                              (PurchaseOrder)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                        delete();
                    }
                    break;
            }
        }
    
        String doShowUIView(FormActionToPerform action)
        {
            String responseURL = "fail.xhtml";
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    responseURL = "purchaseOrder.xhtml";
                    break;
                case SHOW_EDIT_VIEW:
                    setEditMode(true);
                    setComponent(null);
                    responseURL = "purchaseOrder.xhtml";
                    break;
                case SHOW_DELETE_VIEW:
                case SHOW_VIEW_FOR_LIST:
                    if (this.entityObjectList.size() == 0)
                    {
                        setErrorMessage("No orders to display");
                    }
                    responseURL = "purchaseOrderList.xhtml";
                    break;
    
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    responseURL = "viewPurchaseOrder.xhtml";
                    break;
    
                default:
                    responseURL = HOME;
            }
            return responseURL;
        }
    
        public String save()
        {
            String responseURL = "fail.xhtml";
            try
            {
                PurchaseOrder dbPurchaseOrder =
                   purchaseOrderDao.getPurchaseOrderByName(purchaseOrder.getName());
                if(dbPurchaseOrder == null)
                {
                    dbPurchaseOrder = purchaseOrder;
                }
    
                Customer customer = customerDao.getCustomerByName(selectedCustomer);
                dbPurchaseOrder.addToCustomer(customer);
                purchaseOrder = purchaseOrderDao.update(dbPurchaseOrder);
                //replace the not-yet-persisted dbPurchaseOrder object in customer
                //with the persisted purchaseOrderobject returned from the update()
                //call above.
                customer.removePurchaseOrder(dbPurchaseOrder);
                customer.addPurchaseOrder(purchaseOrder);
                customerDao.update(customer);
                System.out.println("# of Purchase orders for customer: "+
                            purchaseOrder.getCustomer().getPurchaseOrders().size());
                //Output: # of Purchase orders for customer: 1
                responseURL = "success.xhtml";
            }
            catch (DatabaseException e)
            {
                e.printStackTrace();
                setErrorMessage(e.toString());
                responseURL = null;
            }
            return responseURL;
        }
    }
    
    @RequestScoped
    public class InvoiceController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private IInvoiceDao invoiceDao;
        @Inject
        private Invoice invoice;
        @EJB
        private IPurchaseOrderDao purchaseOrderDao;
    
        private List<SelectItem> purchaseOrderList;
        private String selectedPurchaseOrder;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == invoice)
            {
                invoice = new Invoice();
                setEditMode(false);
            }
        }
    
        public void processRequest(FormActionToPerform action) throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    {
                        setInvoice((Invoice)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    }
                    break;
                case SHOW_DELETE_VIEW:
                {
                    setInvoice((Invoice)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    delete();
                }
                break;
            }
        }
    
        String doShowUIView(FormActionToPerform action)
        {
            String responseUrl = "fail.xhtml";
            if (null == invoice)
            {
                System.out.println("invoice == null");
                return responseUrl;
            }
            else
            {
                switch (action)
                {
                    case SHOW_ADD_VIEW:
                        responseUrl = "invoice.xhtml";
                        break;
                    case SHOW_EDIT_VIEW:
                        setEditMode(true);
                        setComponent(null);
                        responseUrl = "invoice.xhtml";
                        break;
                    case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                        responseUrl = "viewInvoice.xhtml";
                        break;
                    case SHOW_DELETE_VIEW:
                    case SHOW_VIEW_FOR_LIST:
                        if (this.entityObjectList.size() == 0)
                        {
                            setErrorMessage("no invoices to display");
                        }
                        responseUrl = "invoiceList.xhtml";
                        break;
                    default:
                        responseUrl = "index.xhtml";
                }
            }
            return responseUrl;
        }
    
        public String save()
        {
            String responseUrl = "fail.xhtml";
            try
            {
                Invoice dbInvoice = invoiceDao.getInvoiceByName(invoice.getName());
                if(dbInvoice == null)
                {
                    //this is a new invoice
                    dbInvoice = invoice;
                }
                PurchaseOrder purchaseOrder =
                     purchaseOrderDao.getPurchaseOrderByName(selectedPurchaseOrder);
                dbInvoice.addToPurchaseOrder(purchaseOrder);
                invoice = invoiceDao.update(dbInvoice);
                //replace the not-yet-persisted dbInvoice object in purchaseOrder
                //with the persisted invoice object returned from the update() call above.
                purchaseOrder.removeInvoice(dbInvoice);
                purchaseOrder.addInvoice(invoice);
                purchaseOrderDao.update(purchaseOrder);
                System.out.println("# of Invoices for purchase order: "+
                                   invoice.getPurchaseOrder().getInvoices().size());
                //Output: # of Invoices for purchase order: 1
                responseUrl = "success.xhtml";
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
                e.printStackTrace();
                setErrorMessage(e.toString());
            }
            return responseUrl;
        }
    }
    
    public abstract class FormRequestController implements NavigationConstants
    {
    
        protected enum FormActionToPerform {
            SHOW_ADD_VIEW,
            SHOW_EDIT_VIEW,
            SHOW_DELETE_VIEW,
            SHOW_VIEW_TO_VIEW_SELECTED_OBJECT,
            SHOW_VIEW_FOR_LIST;
        }
    
        protected FacesContext context;
        protected List<?> entityObjectList;
        private UIComponent component;
        protected boolean editMode;
        protected String componentId = null;
    
        public String showViewDataTable() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_VIEW_FOR_LIST);
            return doShowUIView(FormActionToPerform.SHOW_VIEW_FOR_LIST);
        }
    
        public String showViewToAdd() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_ADD_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_ADD_VIEW);
        }
    
        public String showViewToEdit() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_EDIT_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_EDIT_VIEW);
        }
    
        public String showViewToDeleteDetails() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_DELETE_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_DELETE_VIEW);
        }
    
        public String showViewToViewDetails() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_VIEW_TO_VIEW_SELECTED_OBJECT);
            return doShowUIView(FormActionToPerform.SHOW_VIEW_TO_VIEW_SELECTED_OBJECT);
        }
    
        abstract String doShowUIView(FormActionToPerform action);
        abstract void processRequest(FormActionToPerform action) throws DatabaseException;
    
        protected void bindData() {
    
        }
    
        abstract String save();
        abstract void delete() throws DatabaseException;
    
        public List<?> getEntityObjectList() {
            return entityObjectList;
        }
    
        public void setEntityObjectList(List<?> entityObjectList) {
            this.entityObjectList = entityObjectList;
        }
    
        public FacesContext getContext() {
            setContext(FacesContext.getCurrentInstance());
            return context;
        }
    
        public void setContext(FacesContext context) {
            this.context = context;
        }
    
        public UIComponent getComponent() {
            return component;
        }
    
        public void setComponent(UIComponent component) {
            this.component = component;
        }
    }
    
    @RequestScoped
    公共类PurchaseOrderController扩展FormRequestController
    {
    @注入
    私有HTMLDataTableActionBean HTMLDataTableActionBean;
    @EJB
    私有IPURCHASEORDEDAO PURCHASEORDEDAO;
    @EJB
    私人发票;
    @EJB
    私人ICCustomerDao customerDao;
    @注入
    私人采购订单;
    私人名单客户名单;
    私有字符串selectedCustomer;
    @施工后
    public void init()引发DatabaseException
    {
    setEntityObjectList(findAll());
    if(null==采购订单)
    {
    purchaseOrder=新的purchaseOrder();
    setEditMode(假);
    }
    }
    公共作废处理请求(FormActionToPerform操作)
    抛出数据库异常
    {
    开关(动作)
    {
    案例显示\添加\视图:
    setPurchaseOrder(新的PurchaseOrder());
    打破
    案例显示\u视图\u用于\u列表:
    setEntityObjectList(findAll());
    打破
    案例显示\编辑\视图:
    案例显示\u视图\u至\u视图\u所选\u对象:
    {
    设置采购订单(
    (PurchaseOrder)getHtmlDataTableActionBean()。
    getSelectedEntityObject());
    }
    打破
    案例显示\删除\视图:
    {
    设置采购订单(
    (PurchaseOrder)getHtmlDataTableActionBean()。
    getSelectedEntityObject());
    删除();
    }
    打破
    }
    }
    字符串doShowUIView(FormActionToPerform操作)
    {
    字符串responseURL=“fail.xhtml”;
    开关(动作)
    {
    案例显示\添加\视图:
    responseURL=“purchaseOrder.xhtml”;
    打破
    案例显示\编辑\视图:
    setEditMode(真);
    setComponent(null);
    responseURL=“purchaseOrder.xhtml”;
    打破
    案例显示\删除\视图:
    案例显示\u视图\u用于\u列表:
    if(this.entityObjectList.size()==0)
    {
    setErrorMessage(“无需显示订单”);
    }
    responseURL=“purchaseOrderList.xhtml”;
    打破
    案例显示\u视图\u至\u视图\u所选\u对象:
    responseURL=“viewPurchaseOrder.xhtml”;
    打破
    违约:
    responseURL=家;
    }
    返回应答器;
    }
    公共字符串保存()
    {
    字符串responseURL=“fail.xhtml”;
    尝试
    {
    采购订单数据库采购订单=
    getPurchaseOrderByName(purchaseOrder.getName());
    if(dbPurchaseOrder==null)
    {
    dbPurchaseOrder=采购订单;
    }
    Customer Customer=customerDao.getCustomerByName(selectedCustomer);
    dbPurchaseOrder.addToCustomer(客户);
    purchaseOrder=purchaseOrderDao.update(dbPurchaseOrder);
    //替换customer中尚未持久化的dbPurchaseOrder对象
    //使用从更新()返回的持久化purchaseOrderobject
    //给上面打电话。
    customer.removePurchaseOrder(dbPurchaseOrder);
    customer.addPurchaseOrder(purchaseOrder);
    客户道更新(客户);
    System.out.println(“#客户的采购订单:”+
    getCustomer().getPurchaseOrders().size());
    //输出:#客户的采购订单:1
    responseURL=“success.xhtml”;
    }
    捕获(数据库异常)
    {
    e、 printStackTrace();
    setErrorMessage(例如toString());
    responseURL=null;
    }
    返回应答器;
    }
    }
    
    发票控制器

    @RequestScoped
    public class CustomerController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private ICustomerDao customerDao;
        @Inject
        private Customer customer;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == this.getCustomer())
            {
                setCustomer(new Customer());
            }
        }
    
        public void processRequest(FormActionToPerform action) throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    setCustomer(new Customer());
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    setCustomer((Customer) getHtmlDataTableActionBean()
                            .getSelectedEntityObject());
                    break;
                case SHOW_DELETE_VIEW:
                    setCustomer((Customer) getHtmlDataTableActionBean()
                            .getSelectedEntityObject());
                    delete();
                    break;
            }
        }
    
        public String doShowUIView(FormActionToPerform action)
        {
            String responseURL = "fail.xhtml";
            if (null == this.customer)
            {
                return responseURL;
            }
            else
            {
                switch (action)
                {
                    case SHOW_ADD_VIEW:
                        responseURL = "customer.xhtml";
                        break;
                    case SHOW_EDIT_VIEW:
                        responseURL = "customer.xhtml";
                        break;
                    case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                        responseURL = "viewCustomer.xhtml";
                        break;
                    case SHOW_DELETE_VIEW:
                        responseURL = "customerList.xhtml";
                        break;
                    case SHOW_VIEW_FOR_LIST:
                        if (this.entityObjectList.size() == 0)
                        {
                            setErrorMessage("No customers to display");
                        }
                        responseURL = "customerList.xhtml";
                        break;
                    default:
                        responseURL = "index.xhtml";
                }
            }
            return responseURL;
        }
    
        public String save()
        {
            String url = "success.xhtml";
            Customer existingCustomer = null;
            try
            {
                existingCustomer =
                        customerDao.getCustomerByName(this.getCustomer().getName());
    
                if(existingCustomer != null)
                {
                    //there's already a customer with this name, don't make a new one
                    setErrorMessage("Customer already exists");
                    url = "fail.xhtml";
                }
    
                customerDao.update(customer);
            }
            catch (DatabaseException e)
            {
                setErrorMessage(e.toString());
                e.printStackTrace();
                url = "fail.xhtml";
            }
            return url;
        }
    }
    
    @RequestScoped
    public class PurchaseOrderController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private IPurchaseOrderDao purchaseOrderDao;
        @EJB
        private IInvoiceDao invoiceDao;
        @EJB
        private ICustomerDao customerDao;
        @Inject
        private PurchaseOrder purchaseOrder;
    
        private List<SelectItem> customerList;
        private String selectedCustomer;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == purchaseOrder)
            {
                purchaseOrder = new PurchaseOrder();
                setEditMode(false);
            }
        }
    
        public void processRequest(FormActionToPerform action)
        throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    setPurchaseOrder(new PurchaseOrder());
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    {
                        setPurchaseOrder(
                              (PurchaseOrder)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    }
                    break;
                case SHOW_DELETE_VIEW:
                    {
                        setPurchaseOrder(
                              (PurchaseOrder)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                        delete();
                    }
                    break;
            }
        }
    
        String doShowUIView(FormActionToPerform action)
        {
            String responseURL = "fail.xhtml";
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    responseURL = "purchaseOrder.xhtml";
                    break;
                case SHOW_EDIT_VIEW:
                    setEditMode(true);
                    setComponent(null);
                    responseURL = "purchaseOrder.xhtml";
                    break;
                case SHOW_DELETE_VIEW:
                case SHOW_VIEW_FOR_LIST:
                    if (this.entityObjectList.size() == 0)
                    {
                        setErrorMessage("No orders to display");
                    }
                    responseURL = "purchaseOrderList.xhtml";
                    break;
    
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    responseURL = "viewPurchaseOrder.xhtml";
                    break;
    
                default:
                    responseURL = HOME;
            }
            return responseURL;
        }
    
        public String save()
        {
            String responseURL = "fail.xhtml";
            try
            {
                PurchaseOrder dbPurchaseOrder =
                   purchaseOrderDao.getPurchaseOrderByName(purchaseOrder.getName());
                if(dbPurchaseOrder == null)
                {
                    dbPurchaseOrder = purchaseOrder;
                }
    
                Customer customer = customerDao.getCustomerByName(selectedCustomer);
                dbPurchaseOrder.addToCustomer(customer);
                purchaseOrder = purchaseOrderDao.update(dbPurchaseOrder);
                //replace the not-yet-persisted dbPurchaseOrder object in customer
                //with the persisted purchaseOrderobject returned from the update()
                //call above.
                customer.removePurchaseOrder(dbPurchaseOrder);
                customer.addPurchaseOrder(purchaseOrder);
                customerDao.update(customer);
                System.out.println("# of Purchase orders for customer: "+
                            purchaseOrder.getCustomer().getPurchaseOrders().size());
                //Output: # of Purchase orders for customer: 1
                responseURL = "success.xhtml";
            }
            catch (DatabaseException e)
            {
                e.printStackTrace();
                setErrorMessage(e.toString());
                responseURL = null;
            }
            return responseURL;
        }
    }
    
    @RequestScoped
    public class InvoiceController extends FormRequestController
    {
        @Inject
        private HTMLDataTableActionBean htmlDataTableActionBean;
        @EJB
        private IInvoiceDao invoiceDao;
        @Inject
        private Invoice invoice;
        @EJB
        private IPurchaseOrderDao purchaseOrderDao;
    
        private List<SelectItem> purchaseOrderList;
        private String selectedPurchaseOrder;
    
        @PostConstruct
        public void init() throws DatabaseException
        {
            setEntityObjectList(findAll());
            if (null == invoice)
            {
                invoice = new Invoice();
                setEditMode(false);
            }
        }
    
        public void processRequest(FormActionToPerform action) throws DatabaseException
        {
            switch (action)
            {
                case SHOW_ADD_VIEW:
                    break;
                case SHOW_VIEW_FOR_LIST:
                    setEntityObjectList(findAll());
                    break;
                case SHOW_EDIT_VIEW:
                case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                    {
                        setInvoice((Invoice)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    }
                    break;
                case SHOW_DELETE_VIEW:
                {
                    setInvoice((Invoice)getHtmlDataTableActionBean().
                                                         getSelectedEntityObject());
                    delete();
                }
                break;
            }
        }
    
        String doShowUIView(FormActionToPerform action)
        {
            String responseUrl = "fail.xhtml";
            if (null == invoice)
            {
                System.out.println("invoice == null");
                return responseUrl;
            }
            else
            {
                switch (action)
                {
                    case SHOW_ADD_VIEW:
                        responseUrl = "invoice.xhtml";
                        break;
                    case SHOW_EDIT_VIEW:
                        setEditMode(true);
                        setComponent(null);
                        responseUrl = "invoice.xhtml";
                        break;
                    case SHOW_VIEW_TO_VIEW_SELECTED_OBJECT:
                        responseUrl = "viewInvoice.xhtml";
                        break;
                    case SHOW_DELETE_VIEW:
                    case SHOW_VIEW_FOR_LIST:
                        if (this.entityObjectList.size() == 0)
                        {
                            setErrorMessage("no invoices to display");
                        }
                        responseUrl = "invoiceList.xhtml";
                        break;
                    default:
                        responseUrl = "index.xhtml";
                }
            }
            return responseUrl;
        }
    
        public String save()
        {
            String responseUrl = "fail.xhtml";
            try
            {
                Invoice dbInvoice = invoiceDao.getInvoiceByName(invoice.getName());
                if(dbInvoice == null)
                {
                    //this is a new invoice
                    dbInvoice = invoice;
                }
                PurchaseOrder purchaseOrder =
                     purchaseOrderDao.getPurchaseOrderByName(selectedPurchaseOrder);
                dbInvoice.addToPurchaseOrder(purchaseOrder);
                invoice = invoiceDao.update(dbInvoice);
                //replace the not-yet-persisted dbInvoice object in purchaseOrder
                //with the persisted invoice object returned from the update() call above.
                purchaseOrder.removeInvoice(dbInvoice);
                purchaseOrder.addInvoice(invoice);
                purchaseOrderDao.update(purchaseOrder);
                System.out.println("# of Invoices for purchase order: "+
                                   invoice.getPurchaseOrder().getInvoices().size());
                //Output: # of Invoices for purchase order: 1
                responseUrl = "success.xhtml";
            }
            catch (Exception e)
            {
                System.out.println(e.toString());
                e.printStackTrace();
                setErrorMessage(e.toString());
            }
            return responseUrl;
        }
    }
    
    public abstract class FormRequestController implements NavigationConstants
    {
    
        protected enum FormActionToPerform {
            SHOW_ADD_VIEW,
            SHOW_EDIT_VIEW,
            SHOW_DELETE_VIEW,
            SHOW_VIEW_TO_VIEW_SELECTED_OBJECT,
            SHOW_VIEW_FOR_LIST;
        }
    
        protected FacesContext context;
        protected List<?> entityObjectList;
        private UIComponent component;
        protected boolean editMode;
        protected String componentId = null;
    
        public String showViewDataTable() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_VIEW_FOR_LIST);
            return doShowUIView(FormActionToPerform.SHOW_VIEW_FOR_LIST);
        }
    
        public String showViewToAdd() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_ADD_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_ADD_VIEW);
        }
    
        public String showViewToEdit() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_EDIT_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_EDIT_VIEW);
        }
    
        public String showViewToDeleteDetails() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_DELETE_VIEW);
            return doShowUIView(FormActionToPerform.SHOW_DELETE_VIEW);
        }
    
        public String showViewToViewDetails() throws DatabaseException {
            processRequest(FormActionToPerform.SHOW_VIEW_TO_VIEW_SELECTED_OBJECT);
            return doShowUIView(FormActionToPerform.SHOW_VIEW_TO_VIEW_SELECTED_OBJECT);
        }
    
        abstract String doShowUIView(FormActionToPerform action);
        abstract void processRequest(FormActionToPerform action) throws DatabaseException;
    
        protected void bindData() {
    
        }
    
        abstract String save();
        abstract void delete() throws DatabaseException;
    
        public List<?> getEntityObjectList() {
            return entityObjectList;
        }
    
        public void setEntityObjectList(List<?> entityObjectList) {
            this.entityObjectList = entityObjectList;
        }
    
        public FacesContext getContext() {
            setContext(FacesContext.getCurrentInstance());
            return context;
        }
    
        public void setContext(FacesContext context) {
            this.context = context;
        }
    
        public UIComponent getComponent() {
            return component;
        }
    
        public void setComponent(UIComponent component) {
            this.component = component;
        }
    }
    
    @RequestScoped
    公共类InvoiceController扩展FormRequestController
    {
    @注入
    私有HTMLDataTableActionBean HTMLDataTableActionBean;
    @EJB
    私人发票;
    @注入
    私人发票;
    @EJB
    私有IPURCHASEORDEDAO PURCHASEORDEDAO;
    私有列表purchaseOrderList;
    私有字符串selectedPurchaseOrder;
    @施工后
    public void init()引发DatabaseException
    {
    setEntityObjectList(findAll());
    如果(空==发票)
    {
    发票=新发票();
    setEditMode(假);
    }
    }
    public void processRequest(FormActionToPerform操作)引发DatabaseException
    {
    开关(动作)
    {
    
    if(!purchaseOrder.getInvoices().contains(this))
    {
        purchaseOrder.addInvoice(this);
    }