/**

        * 登録処理(複数件)

        * @param List<Invoice> invoiceList

        * @throws Exception

        */

       public void insert(List<Invoice> invoiceList)  {

              PersistenceManager pm = PMF.get().getPersistenceManager();

              Transaction tx = pm.currentTransaction();

              int NUM_RETRIES = 3;

             

              try {

              for (int r = 0; r < NUM_RETRIES; r++) {

                     try {

                     tx.begin();

                     logger.info("tx begin.");

                    

                     InvoiceBase invoiceBase;

                     boolean invoiceBaseIsNew = true;

                     try {

                            invoiceBase = (InvoiceBase) pm.getObjectById(InvoiceBase.class,keyUtils.getBaseKeyBuilder().getKey());

                            invoiceBaseIsNew = false;

                           

                     }catch(JDOObjectNotFoundException e){

                            invoiceBase = new InvoiceBase();

                            invoiceBase.setKey(keyUtils.getBaseKeyBuilder().getKey());

                     }

 

                     for (Invoice invoice:invoiceList) {

                     if (invoice!=null) {

                            invoiceBase.setInvoiceId(invoiceBase.getInvoiceId()+1);

                            invoice.setId(invoiceBase.getInvoiceId());

 

                            // invoiceinvoiceNoPKなのでそれを入れている

                            invoice.setKey(keyUtils.getChildKey(Invoice.class,invoice.invoiceNo));

                           

                            List<Order> orderList = invoice.getOrderList();

                            if (orderList!=null&&orderList.size()>0) {

 

                                   long orderid = invoiceBase.getOrderId();

                                   for (Order order:orderList) {

                                          orderid++;

                                          order.setId(orderid);

 

                                          // keyの生成  

                                          KeyFactory.Builder keybuiler = keyUtils.getChildKeyBuilder(Invoice.class,invoice.invoiceNo);

                                          Key key = (keybuiler.addChild(Order.class.getSimpleName(), keyUtils.getRecordKeyName(orderid))).getKey();

                                          order.setKey(key);

 

                                          order.setInvoiceNo(invoice.invoiceNo);

                                   }

                                   invoiceBase.setOrderId(orderid);

 

                            }

                            // これでinvoicepersistent

                            invoiceBase.addInvoice(invoice);

                     }

                     }

                    

                     if (invoiceBaseIsNew) {

                            pm.makePersistent(invoiceBase);

                     }

 

                     logger.info("commit.");

                     tx.commit();

                     break;

 

              }

              catch (JDOCanRetryException e) {

                     if (r == (NUM_RETRIES - 1)) {

                            throw e;

                     }

                     logger.info("rollbacked.");

                     try {

                            tx.rollback();

                     } catch (Throwable ee) {}

              }

              catch (JDOException e) {

                     logger.info("JDOException:"+e.getMessage());

                     throw e;

              }

              catch (ConcurrentModificationException e) {

                     logger.info("ConcurrentModificationException:"+e.getMessage());

                     throw e;

              }

              } // for loop

 

              }finally {

                     if (tx.isActive()) {

                            logger.info("rollbacked.");

                            try {

                                   tx.rollback();

                            } catch (Throwable e) {}

                     }

                     logger.info("pm closed.");

                     try {

                            pm.close();

                     } catch (Throwable e) {}

              }

       }

 

       /**

        * 登録処理(1件)

        * @param invoice invoice

        * @throws Exception

        */

       public void insert(Invoice invoice)  {

              List<Invoice> invoiceList = new ArrayList<Invoice>();

              invoiceList.add(invoice);

              insert(invoiceList);

       }