diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Query.php b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
index 1d56bf1..634009e 100644
--- a/core/lib/Drupal/Core/Config/Entity/Query/Query.php
+++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
@@ -43,9 +43,11 @@ class Query extends QueryBase implements QueryInterface {
    *   The entity manager that stores all meta information.
    * @param \Drupal\Core\Config\StorageInterface $config_storage
    *   The actual config storage which is used to list all config items.
+   * @param array $namespaces
+   *   List of potential namespaces of the classes belonging to this query.
    */
-  function __construct($entity_type, $conjunction, EntityManager $entity_manager, StorageInterface $config_storage) {
-    parent::__construct($entity_type, $conjunction);
+  function __construct($entity_type, $conjunction, EntityManager $entity_manager, StorageInterface $config_storage, array $namespaces) {
+    parent::__construct($entity_type, $conjunction, $namespaces);
     $this->entityManager = $entity_manager;
     $this->configStorage = $config_storage;
   }
diff --git a/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php b/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php
index 872a8a7..76463ad 100644
--- a/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php
+++ b/core/lib/Drupal/Core/Config/Entity/Query/QueryFactory.php
@@ -7,9 +7,9 @@
 
 namespace Drupal\Core\Config\Entity\Query;
 
-use Drupal\Core\Cache\CacheBackendInterface;
 use Drupal\Core\Config\StorageInterface;
 use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Entity\Query\QueryBase;
 use Drupal\Core\Entity\Query\QueryException;
 use Drupal\Core\Entity\Query\QueryFactoryInterface;
 
@@ -21,11 +21,18 @@ class QueryFactory implements QueryFactoryInterface {
   /**
    * The config storage used by the config entity query.
    *
-   * @var \Drupal\Core\Cache\CacheBackendInterface
+   * @var \Drupal\Core\Config\StorageInterface;
    */
   protected $configStorage;
 
   /**
+   * The namespace of this class, the parent class etc.
+   *
+   * @var array
+   */
+  protected $namespaces;
+
+  /**
    * Constructs a QueryFactory object.
    *
    * @param \Drupal\Core\Config\StorageInterface $config_storage
@@ -33,13 +40,14 @@ class QueryFactory implements QueryFactoryInterface {
    */
   public function __construct(StorageInterface $config_storage) {
     $this->configStorage = $config_storage;
+    $this->namespaces = QueryBase::getNamespaces($this);
   }
 
   /**
    * {@inheritdoc}
    */
   public function get($entity_type, $conjunction, EntityManager $entity_manager) {
-    return new Query($entity_type, $conjunction, $entity_manager, $this->configStorage);
+    return new Query($entity_type, $conjunction, $entity_manager, $this->configStorage, $this->namespaces);
   }
 
   /**
diff --git a/core/lib/Drupal/Core/Entity/Query/QueryBase.php b/core/lib/Drupal/Core/Entity/Query/QueryBase.php
index d3b7b66..4e9b8ec 100644
--- a/core/lib/Drupal/Core/Entity/Query/QueryBase.php
+++ b/core/lib/Drupal/Core/Entity/Query/QueryBase.php
@@ -119,11 +119,19 @@
   protected $pager = array();
 
   /**
+   * List of potential namespaces of the classes belonging to this query.
+   *
+   * @var array
+   */
+  protected $namespaces = array();
+
+  /**
    * Constructs this object.
    */
-  public function __construct($entity_type, $conjunction) {
+  public function __construct($entity_type, $conjunction, array $namespaces) {
     $this->entityType = $entity_type;
     $this->conjunction = $conjunction;
+    $this->namespaces = $namespaces;
     $this->condition = $this->conditionGroupFactory($conjunction);
     if ($this instanceof QueryAggregateInterface) {
       $this->conditionAggregate = $this->conditionAggregateGroupFactory($conjunction);
@@ -184,9 +192,11 @@ public function range($start = NULL, $length = NULL) {
    * @return \Drupal\Core\Entity\Query\ConditionInterface
    *   An object holding a group of conditions.
    */
-  protected function conditionGroupFactory($conjunction = 'AND') {
-    $class = static::getNamespace($this) . '\\Condition';
-    return new $class($conjunction, $this);
+  protected function conditionGroupFactory($conjunction = 'AND') {
+    // As the factory classes hardwire QueryBase::getClass, it needs to be
+    // hardwired here too.
+    $class = QueryBase::getClass($this->namespaces, 'Condition');
+    return new $class($conjunction, $this, $this->namespaces);
   }
 
   /**
@@ -422,17 +432,41 @@ protected function getAggregationAlias($field, $function) {
   }
 
   /**
-   * Returns the namespace of an object.
+   * Gets a list of namespaces of the ancestors of a class.
    *
    * @param $object
-   *   The object.
+   *   An object within a namespace.
+   *
+   * @return array
+   *   A list containing the namespace of the class, the namespace of the
+   *   parent of the class and so on and so on.
+   */
+  public static function getNamespaces($object) {
+    $namespaces = array();
+    for ($class = get_class($object); $class; $class = get_parent_class($class)) {
+      $namespaces[] = substr($class, 0, strrpos($class, '\\'));
+    }
+    return $namespaces;
+  }
+
+  /**
+   * Finds a class in a list of namespaces.
+   *
+   * @param array $namespaces
+   *   A list of namespaces.
+   * @param string $short_class_name
+   *   A class name without namespace.
    *
    * @return string
-   *   The namespace.
+   *   The fully qualified name of the class.
    */
-  public static function getNamespace($object) {
-    $class = get_class($object);
-    return substr($class, 0, strrpos($class, '\\'));
+  public static function getClass(array $namespaces, $short_class_name) {
+    foreach ($namespaces as $namespace) {
+      $class = $namespace . '\\' . $short_class_name;
+      if (class_exists($class)) {
+        return $class;
+      }
+    }
   }
 
 }
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
index debc403..615e8b7 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/Query.php
@@ -80,8 +80,8 @@ class Query extends QueryBase implements QueryInterface {
    * @param \Drupal\Core\Database\Connection $connection
    *   The database connection to run the query against.
    */
-  public function __construct($entity_type, EntityManager $entity_manager, $conjunction, Connection $connection) {
-    parent::__construct($entity_type, $conjunction, $connection);
+  public function __construct($entity_type, EntityManager $entity_manager, $conjunction, Connection $connection, array $namespaces) {
+    parent::__construct($entity_type, $conjunction, $namespaces);
     $this->entityManager = $entity_manager;
     $this->connection = $connection;
   }
@@ -324,7 +324,7 @@ public function __clone() {
    *   The object that adds tables and fields to the SQL query object.
    */
   public function getTables(SelectInterface $sql_query) {
-    $class = QueryBase::getNamespace($this) . '\\Tables';
+    $class = QueryBase::getClass($this->namespaces, 'Tables');
     return new $class($sql_query);
   }
 
diff --git a/core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php b/core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php
index 31336d0..4901099 100644
--- a/core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php
+++ b/core/lib/Drupal/Core/Entity/Query/Sql/QueryFactory.php
@@ -28,6 +28,13 @@ class QueryFactory implements QueryFactoryInterface {
   protected $connection;
 
   /**
+   * The namespace of this class, the parent class etc.
+   *
+   * @var array
+   */
+  protected $namespaces;
+
+  /**
    * Constructs a QueryFactory object.
    *
    * @param \Drupal\Core\Database\Connection $connection
@@ -35,6 +42,7 @@ class QueryFactory implements QueryFactoryInterface {
    */
   public function __construct(Connection $connection) {
     $this->connection = $connection;
+    $this->namespaces = QueryBase::getNamespaces($this);
   }
 
   /**
@@ -50,8 +58,8 @@ public function __construct(Connection $connection) {
    *   The factored query.
    */
   public function get($entity_type, $conjunction, EntityManager $entity_manager) {
-    $class = QueryBase::getNamespace($this) . '\\Query';
-    return new $class($entity_type, $entity_manager, $conjunction, $this->connection);
+    $class = QueryBase::getClass($this->namespaces, 'Query');
+    return new $class($entity_type, $entity_manager, $conjunction, $this->connection, $this->namespaces);
   }
 
   /**
@@ -67,8 +75,8 @@ public function get($entity_type, $conjunction, EntityManager $entity_manager) {
    *   The factored aggregation query.
    */
   public function getAggregate($entity_type, $conjunction, EntityManager $entity_manager) {
-    $class = QueryBase::getNamespace($this) . '\\QueryAggregate';
-    return new $class($entity_type, $entity_manager, $conjunction, $this->connection);
+    $class = QueryBase::getClass($this->namespaces, 'QueryAggregate');
+    return new $class($entity_type, $entity_manager, $conjunction, $this->connection, $this->namespaces);
   }
 
 }
