How to attach a paragraph to a node using node_updatehook? I am trying to attach a paragraph, to a node. I am currently trying to do it in node_update hook. 

The value for paragraph will come a custom field. Currently am trying to achieve this with hard-coded value. I tried two methods, but both are not working.

Method 1

function my_module_node_update(Drupal\Core\Entity\EntityInterface $entity) {

  $paragraph = Paragraph::create([
  'type' =&gt; 'attendance',
  'field_stakeholder' =&gt; array(
      "value"  =&gt;  '. Roshni, UPS Kharwna from Dunda, Uttarkashi (UTK_130000377) (49645)',
    ),
  ]);

  $paragraph-&gt;save(); 

//this is working
 dpm($paragraph-&gt;id());

 $current[] = array(
      'target_id' =&gt; $paragraph-&gt;id(),
      'target_revision_id' =&gt; $paragraph-&gt;getRevisionId(),
    );



   $entity-&gt;set('field_attendance', $current);  
   $entity-&gt;save();

}


Method 2

function my_module_node_update(Drupal\Core\Entity\EntityInterface $entity) {

  $paragraph = Paragraph::create([
  'type' =&gt; 'attendance',
  'field_stakeholder' =&gt; array(
      "value"  =&gt;  '. Roshni, UPS Kharwna from Dunda, Uttarkashi (UTK_130000377) (49645)',
    ),
  ]);

  $paragraph-&gt;save(); 

//this is working
 dpm($paragraph-&gt;id());

   $entity-&gt;field_attendance-&gt;appendItem($paragraph);
   $entity-&gt;save();

}


However, I am getting an error:


  Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity
  constraint violation: 1062 Duplicate entry '213436' for key 'PRIMARY':
  INSERT INTO {node_revision} (nid, vid, langcode, revision_timestamp,
  revision_uid, revision_log) VALUES (:db_insert_placeholder_0,
  :db_insert_placeholder_1, :db_insert_placeholder_2,
  :db_insert_placeholder_3, :db_insert_placeholder_4,
  :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => 41374
  [:db_insert_placeholder_1] => 213436 [:db_insert_placeholder_2] => en
  [:db_insert_placeholder_3] => 1535117690 [:db_insert_placeholder_4] =>
  1 [:db_insert_placeholder_5] => ) in
  Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 777 of
  /home/rupali/fa-rupali/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).


I feel like the error is coming as the hook is called twice. The code is working till paragraph creation. I am able to verify it.
I used this code in hooky_entity_presave

       $paragraph = Paragraph::create([
              'type' =&gt; 'attendance',
              'field_stakeholder' =&gt; array(
               "value"  =&gt;  '". Seema Bist, PS Manjkot from Dunda, Uttarkashi (UTK_130000379) (49647)"',
               //"value"  =&gt;  '49645'
            ),
          ]);
              $paragraph-&gt;save();


            $entity-&gt;field_attendance-&gt;appendItem($paragraph);


Paragragh array is display (target id), but didn't display value of target id  in input value.
in 
Means data added in node__field_at table, but not target id with node id is doesnt add in paragraph__field_st (st is entity reference field in paragraph type.

How to save data in paragraph entity reference field