title: Lazy initialisation and Spring Data DomainClassConverter
body: <p>I'm trying to use the DomainClassConverter from Spring Data to load entities in a controller and then use these entities in a service. </p>
  
  <p>The problem is that I get a LazyInitializationException when I access lazy loaded collection from my Service. </p>
  
  <p>Adding Transactional annotation to the controller does not help, I guess the conversion occurs before the start of the controller transaction.</p>
  
  <p>Is there a way to use this converter in this kind of use case ? can I reattach the entity to the current session someway ?</p>
  
  <p>My controller:</p>
  
  <pre><code>@RestController
  @RequestMapping("/api/quotation-requests/{id}/quotation")
  public class QuotationResource {
  
      @RequestMapping(value = "/lines", method = RequestMethod.POST, params="freeEntry")
      @Timed
      public ResponseEntity&lt;PricingLineDTO&gt; addFreeEntryLine(@PathVariable("id") Quotation quotation, @RequestBody PricingLineDTO pricingLineTo)
      {
          PricingLine pricingLine = conversionService.convert(pricingLineTo, PricingLine.class);
  
          pricingLine = quotationService.addFreeLineToQuotation(quotation, pricingLine);
  
          return new ResponseEntity&lt;&gt;(conversionService.convert(pricingLine, PricingLineDTO.class), HttpStatus.OK);
      }
  }
  </code></pre>
  
  <p>The service:</p>
  
  <pre><code>@Service
  @Transactional
  public class QuotationService {
  
      public PricingLine addFreeLineToQuotation(Quotation quotation, PricingLine pricingLine) {
          quotation.getPricingLines().add(pricingLine); // org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: x.y.z.Quotation.pricingLines, could not initialize proxy
      }
  }
  </code></pre>
  
  <p>And the entity</p>
  
  <pre><code>@Entity
  public class Quotation {
  
      @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
      private List&lt;PricingLine&gt; pricingLines = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>If it is not possible, what is my best alternative :</p>
  
  <ul>
  <li>make the controller transactionnal, inject data repositories in it and still offer a Service API that takes Entities parameters instead of IDs. </li>
  </ul>
  
  <p>Cons: controller become transactional, which seem to be commonly considered as a bad practice; it introduces boilerplate code.</p>
  
  <ul>
  <li>make the Service API takes IDs as parameters, letting the controller out of the transaction.</li>
  </ul>
  
  <p>Cons: The API become harder to read and can be error prone as every entities as referred as "Long" object, especially when a Service method needs severals entities as input. For example:</p>
  
  <pre><code>void addUserToGroup(Long userId, Long groupId)
  </code></pre>
  
  <p>One could easily switch parameters.</p>