title: Avoiding loading LAZY collection in Spring Data Jpa
body: <p>I have following relationship:</p>
  
  <pre><code>@Entity class Shop {
  @OneToMany(mappedBy = "shop", fetch = LAZY)
  private List&lt;Employee&gt; employees = new LinkedList&lt;&gt;();
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Entity class Employee {
  @ManyToOne
  private Shop shop;
  }
  </code></pre>
  
  <p>I've declared Spring Data repository like this:</p>
  
  <pre><code>public interface ShopRepository extends JpaRepository&lt;Shop, Long&gt; {}
  </code></pre>
  
  <p>Calling <code>ShopRepository#findOne(id)</code> method forces fetching of the <code>List&lt;Employee&gt; employees</code> which is <code>LAZY</code> relationship.</p>
  
  <p>I have service which uses Shop repository:</p>
  
  <pre><code>@Service
  @Transactional(readOnly = true)
  public class ShopService {
  
  private final ShopRepository shopRepository;
  
  @Autowired
  public ShopService(ShopRepository shopRepository) {
      this.shopRepository = shopRepository;
  }
  public Shop find(Long id) {
      return shopRepository.findOne(id);
  }
  </code></pre>
  
  <p>}
  The service method is called within another controller method:</p>
  
  <pre><code>@RequestMapping(value = "api/schedule/{shopId:[0-9]+}/{date:\\d{4}-\\d{2}-\\d{2}}", method = RequestMethod.GET)
  @ResponseBody
  public Schedule getSchedule(@PathVariable Long shopId,
                              @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
      Schedule schedule = scheduleService.findSchedule(shopId, date);
      if(schedule != null)
          return schedule;
      else {
          Shop shop = shopService.find(shopId);
          Schedule empty = new Schedule(shop, date);
          return empty;
      }
  }
  </code></pre>
  
  <p><strong>How to get rid of fetching <code>employees</code> relationship?</strong></p>