public static boolean isInvoiceAlreadyStored(Invoice invoice) {
    List<Invoice> results =
        getObjectsByRow(Invoice.class, "po_number", EqualityRelation.E, invoice.getPoNumber());
    boolean emptySet = results.isEmpty();

    if (!emptySet) {
      Invoice oldInvoice = results.get(0);
      invoice.setId(oldInvoice.getId());
      saveOrUpdateInvoice(invoice);
      return true;
    }

    return false;
  }
  private static void saveOrUpdateInvoice(Invoice invoice) {

    updateOrStoreObject(invoice);

    Customer customer = invoice.getCustomer();
    for (Address address : customer.getAddresses()) {
      updateOrStoreObject(address);
    }
    updateOrStoreObject(customer);

    ProductGroup group = invoice.getProductGroup();
    group.setInvoice(invoice);
    updateOrStoreObject(group);
    for (ProductLine line : group.getProductLines()) {
      line.setProductPrice(line.getProduct().getPrice());
      line.setProductGroup(invoice.getProductGroup());
      updateOrStoreObject(line);
    }
  }