How do I retrieve an entity from an entity reference I have a custom module with three entities. The first represents a chemical compound. The second is a list of the periodic table of elements. The third is a many-to-many binding between these two, and also contains a "quantity" field. This last entity is to be able to create each chemical's chemical formula.

Example-combining the quantities and element symbols to produce CH4O

Entity:chemicals
id   name
1    Methanol

Entity:elements
id    symbol   name
1      H       Hydrogen
2      C       Carbon
3      O       Oxygen

Entity:chemical_formulas
id     element    chemical   quantity
1        2          1           1
2        1          1           4
3        3          1           1


I have the three entities working to the point that I can get all the correct data in the database. However, retrieving has proven a challenge.

My ChemicalFormulas class contains this function, and this seems to be where the problem is because it always returns 'null'. The ChemicalFormulas->getQuantity() function is basically the same, and it works fine.

//in the ChemicalFormulas class
public function getElement() {
   return $this-&gt;get('element')-&gt;value;
}


The field is defined as 

$fields['element'] = BaseFieldDefinition::create('entity_reference')
  -&gt;setLabel(t('Element'))
  -&gt;setDescription(t('The Element.'))
  -&gt;setRevisionable(TRUE)
  -&gt;setSetting('target_type', 'elements')
  -&gt;setSetting('handler', 'default')
  -&gt;setDisplayOptions('view', array(
    'label' =&gt; 'above',
    'type' =&gt; 'string',
    'weight' =&gt; 0,
  ))
  -&gt;setDisplayOptions('form', array(
    'type' =&gt; 'entity_reference_autocomplete',
    'weight' =&gt; 5,
    'settings' =&gt; array(
      'match_operator' =&gt; 'CONTAINS',
      'size' =&gt; '60',
      'autocomplete_type' =&gt; 'tags',
      'placeholder' =&gt; '',
    ),
  ))
  -&gt;setDisplayConfigurable('form', TRUE)
  -&gt;setDisplayConfigurable('view', TRUE);


I am trying to produce the formula with this function

public function getFormula(){
  $query = \Drupal::entityQuery('chemical_formulas')
    -&gt;condition('chemical', $this-&gt;id());

  $ids = $query-&gt;execute();
  $entries = entity_load_multiple('chemical_formulas', $ids);
  $formula = "";
  foreach ($entries as $entry){
    //Is element supposed to be an entity or the id of an entity?
    //$element = $entry-&gt;getElement(); //If it's an entity
    $element = entity_load('elements', $entry-&gt;getElement()); //If it's an integer.
    $symbol = $element-&gt;getSymbol();
    $quantity = $entry-&gt;getQuantity();
    if ($quantity == 1) {
      $quantity = "";
    }
    $formula .= $symbol . $quantity;
  }
  return $formula;
}
Entities part: 

I think (as much you showed) your entity definition is wrong: 


Entity names should be always SINGULAR: Chemical, Element, Chemical Formula
Your Chemical Formula should have the ability to hold reference to MULTIPLE elements (H20 for example, has H and O elements). Which means you won't able to store this information in this entity table, but you will have to create an external table to hold this information. You can find example for this in core entities (like taxonomy_terms, if I remember good).
If you make this changes right, you won't have query anything, since the referenced entity ids (elements, chemical) will be present in your entity. 
You only have to load via EntityTypeManager.


Final hint again: always examine core / contrib entities. Node, Taxonomy terms are pretty good examples.