protected function hydrateAssociations($entity, $data)
    {
        if (isset($data['relationships']) && is_array($data['relationships'])) {
            $metadata = $this->entityManager->getClassMetadata(get_class($entity));

            foreach ($data['relationships'] as $name => $data) {
                if (!isset($metadata->associationMappings[$name])) {
                    throw new \Exception(sprintf('Relation `%s` association not found', $name));
                }

                $mapping = $metadata->associationMappings[$name];

                if (is_array($data['data'])) {
                    if ($resourceId = $this->getResourceId($data['data'])) {
                        $this->hydrateToOneAssociation($entity, $name, $mapping, $resourceId);
                    } else {
                        $this->hydrateToManyAssociation($entity, $name, $mapping,
                            $this->mapRelationshipsArray($data['data'])
                        );
                    }
                }
            }
        }

        return $entity;
    }