code: private void mergeInTransaction(EntityManager em, Collection objects) {
          EntityTransaction tx = null;
          try {
              tx = em.getTransaction();
              tx.begin();
              for (Object o : objects) {
                  em.merge(o);
              }
              tx.commit();
          }
          catch (RuntimeException e) {
              if (tx != null && tx.isActive()) {
                  tx.rollback();
              }
              throw e;
          }
      }
docs: Persist collection of objects.
  
  @param em      entity manager used to persist objects
  @param objects objects to persist