/**
  * Batch create entities.
  *
  * Preserves the keys of the request content array as the keys of the
  * response content array. This is helpful for implementations that need to
  * map original identifiers to the newly created entity IDs.
  *
  * There are two outcomes if an exception is thrown during a batch. If
  * continueOnError is set to the request, the current entity is thrown away
  * but the operation continues. Otherwise, all previously created entities
  * are removed.
  *
  * Detaches entities after they've been created to minimize memory usage.
  * Because the entities are detached, this returns resource references
  * (containing only the entity ID) instead of full entity representations.
  *
  * {@inheritDoc}
  */
 public function batchCreate(Request $request)
 {
     $errorStore = new ErrorStore();
     $logger = $this->getServiceLocator()->get('Omeka\\Logger');
     $entities = [];
     foreach ($request->getContent() as $key => $datum) {
         $entityClass = $this->getEntityClass();
         $entity = new $entityClass();
         $subRequest = new Request(Request::CREATE, $request->getResource());
         $subRequest->setContent($datum);
         try {
             $this->hydrateEntity($subRequest, $entity, $errorStore);
         } catch (\Exception $e) {
             if ($request->continueOnError()) {
                 $logger->err((string) $e);
                 continue;
             }
             // Remove previously persisted entities before re-throwing.
             foreach ($entities as $entity) {
                 $this->getEntityManager()->remove($entity);
             }
             $this->getEntityManager()->flush();
             throw $e;
         }
         $this->getEntityManager()->persist($entity);
         $entities[$key] = $entity;
     }
     $this->getEntityManager()->flush();
     $references = [];
     foreach ($entities as $key => $entity) {
         $references[$key] = new ResourceReference($entity, $this);
         $this->getEntityManager()->detach($entity);
     }
     return new Response($references);
 }