diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
index 4665f1c..2b7c3de 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\Core\Field\Plugin\Field\FieldType;
 
+use Drupal\Core\Entity\Entity;
 use Drupal\Core\Entity\EntityTypeInterface;
 use Drupal\Core\Entity\TypedData\EntityDataDefinition;
 use Drupal\Core\Field\FieldDefinitionInterface;
@@ -193,6 +194,9 @@ public function isEmpty() {
     if ($target_id !== NULL) {
       return FALSE;
     }
+    if ($this->entity && $this->entity instanceof Entity && !$this->entity->isNew()) {
+      return FALSE;
+    }
     // Allow auto-create entities.
     if ($this->hasUnsavedEntity()) {
       return FALSE;
@@ -208,6 +212,13 @@ public function preSave() {
       $this->entity->save();
       $this->target_id = $this->entity->id();
     }
+    // Handle the case where an unsaved entity was directly set using the public
+    // 'entity' property and then saved before this entity. In this case
+    // ::hasUnsavedEntity() will return FALSE but $this->target_id will still be
+    // empty.
+    if (empty($this->target_id) && $this->entity) {
+      $this->target_id = $this->entity->id();
+    }
   }
 
   /**
diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php
index 48a67e7..77c3bf0 100644
--- a/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php
+++ b/core/modules/entity_reference/src/Tests/EntityReferenceItemTest.php
@@ -167,4 +167,25 @@ public function testConfigEntityReferenceItem() {
     $entity->save();
   }
 
+  /**
+   * Test saving order sequence doesn't matter.
+   */
+  public function testEntitySaveOrder() {
+    // The term entity is unsaved here.
+    $term = entity_create('taxonomy_term', array(
+      'name' => $this->randomMachineName(),
+      'vid' => $this->term->bundle(),
+      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+    ));
+    $entity = entity_create('entity_test');
+    // Now assign the unsaved term to the field.
+    $entity->field_test_taxonomy_term->entity = $term;
+    $entity->name->value = $this->randomMachineName();
+    // Now save the term.
+    $term->save();
+    // And then the entity.
+    $entity->save();
+    $this->assertEqual($entity->field_test_taxonomy_term->entity->id(), $term->id());
+  }
+
 }
