title: Could not write JSON: failed to lazily initialize a collection of role
body: <p>I try to implement a server REST with java - hibernate - spring, which returns a json.</p>
  
  <p>I have map a many to many relation. </p>
  
  <p>I explain better, I have a supplier that have a list of ingredients, and each ingredient has a list of supplier.</p>
  
  <p>I created the table:</p>
  
  <pre><code>CREATE TABLE supplier_ingredient (
    supplier_id BIGINT,
    ingredient_id BIGINT
  )
  
  
  ALTER TABLE supplier_ingredient ADD CONSTRAINT supplier_ingredient_pkey 
  PRIMARY KEY(supplier_id, ingredient_id);
  
  ALTER TABLE supplier_ingredient ADD CONSTRAINT 
  fk_supplier_ingredient_ingredient_id FOREIGN KEY (ingredient_id) 
  REFERENCES ingredient(id);
  
  ALTER TABLE supplier_ingredient ADD CONSTRAINT 
  fk_supplier_ingredient_supplier_id FOREIGN KEY (supplier_id) REFERENCES 
  supplier(id);
  </code></pre>
  
  <p>Then I have <strong>Ingredient</strong> model:</p>
  
  <pre><code>.....
  .....
  @ManyToMany(mappedBy = "ingredients")
  @OrderBy("created DESC")
  @BatchSize(size = 1000)
  private List&lt;Supplier&gt; suppliers = new ArrayList&lt;&gt;();
  ....
  ....
  </code></pre>
  
  <p>Then I have <strong>Supplier</strong> model:</p>
  
  <pre><code>....
  @ManyToMany
  @JoinTable( name = "supplier_ingredient ", 
          joinColumns = @JoinColumn(name = "supplier_id", referencedColumnName = "id"), 
          inverseJoinColumns = @JoinColumn(name = "ingredient_id", referencedColumnName = "id"), 
          foreignKey = @ForeignKey(name = "fk_supplier_ingredient_supplier_id"))
  @OrderBy("created DESC")
  @Cascade(CascadeType.SAVE_UPDATE)
  @BatchSize(size = 1000)
  private List&lt;Ingredient&gt; ingredients = new ArrayList&lt;&gt;();
  ....
  </code></pre>
  
  <p><strong>Endpoint</strong>:</p>
  
  <pre><code>@RequestMapping(value = "/{supplierId:[0-9]+}", method = RequestMethod.GET)
  @ResponseStatus(value = HttpStatus.OK)
  @ResponseBody
  public SupplierObject get(@PathVariable Long supplierId) {
  
      Supplier supplier = supplierService.get(supplierId);
  
      SupplierObject supplierObject = new SupplierObject (supplier);
  
      return SupplierObject;
  
  }
  </code></pre>
  
  <p><strong>Service</strong></p>
  
  <pre><code>....
  public Supplier get(Long supplierId) {
  
      Supplier supplier = supplierDao.getById(supplierId); (it does entityManager.find(entityClass, id))
  
      if (supplier == null) throw new ResourceNotFound("supplier", supplierId);
  
      return supplier;
  }
  ....
  </code></pre>
  
  <p><strong>SupplierObject</strong></p>
  
  <pre><code>    @JsonIgnoreProperties(ignoreUnknown = true)
      public class SupplierObject extends IdAbstractObject {
  
      public String email;
  
      public String phoneNumber;
  
      public String address;
  
      public String responsible;
  
      public String companyName;
  
      public String vat;
  
      public List&lt;Ingredient&gt; ingredients = new ArrayList&lt;&gt;();
  
      public SupplierObject () {
  
      }
  
      public SupplierObject (Supplier supplier) {
  
          id = supplier.getId();
          email = supplier.getEmail();
          responsible = supplier.getResponsible();
          companyName = supplier.getCompanyName();
          phoneNumber = supplier.getPhone_number();
          ingredients = supplier.getIngredients();
          vat = supplier.getVat();
          address = supplier.getAddress();
  
  
      }
  }
  </code></pre>
  
  <p>And <strong>IdAbstractObject</strong></p>
  
  <pre><code>public abstract class IdAbstractObject{
  
      public Long id;
  
  }
  </code></pre>
  
  <p>My problem is, when I call the endpoint:</p>
  
  <pre><code>http://localhost:8080/supplier/1
  </code></pre>
  
  <p>I got an error:</p>
  
  <blockquote>
    <p>"Could not write JSON: failed to lazily initialize a collection of
    role: myPackage.ingredient.Ingredient.suppliers, could not initialize
    proxy - no Session; nested exception is
    com.fasterxml.jackson.databind.JsonMappingException: failed to lazily
    initialize a collection of role:
    myPackage.ingredient.Ingredient.suppliers, could not initialize proxy
    - no Session (through reference chain: myPackage.supplier.SupplierObject[\"ingredients\"]->org.hibernate.collection.internal.PersistentBag[0]->myPackage.ingredient.Ingredient[\"suppliers\"])"</p>
  </blockquote>
  
  <p>I followed this:</p>
  
  <p><a href="https://stackoverflow.com/questions/21708339/avoid-jackson-serialization-on-non-fetched-lazy-objects/21760361#21760361">Avoid Jackson serialization on non fetched lazy objects</a></p>
  
  <p>Now I haven't the error but in json returned, the ingredients field is null:</p>
  
  <pre><code>{
    "id": 1,
    "email": "mail@gmail.com",
    "phoneNumber": null,
    "address": null,
    "responsible": null,
    "companyName": "Company name",
    "vat": "vat number",
    "ingredients": null
  }
  </code></pre>
  
  <p>but in debug I can see ingredients....</p>
  
  <p><a href="https://i.stack.imgur.com/0vTaB.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/0vTaB.jpg" alt="enter image description here"></a></p>