title: Spring Integration Test for Hibernate.initialized for Lazy Loaded Collections
body: <p>I have a lazy loaded @OneToMany relationship which I have initialized using Hibernate.initialize(). Here are my Entities.</p>
  
  <p><strong>Pet.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "pets")
  public class Pet {
  
  @Column(name = "birth_date")
  private Date birthDate;
  
  @ManyToOne
  @JoinColumn(name = "type_id")
  private PetType type;
  
  @OneToOne
  @JoinColumn(name = "owner_id")
  private Owner owner;
  
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "pet", fetch = FetchType.LAZY)
  private Set&lt;Visit&gt; visits;
  </code></pre>
  
  <p>}</p>
  
  <p><strong>Visit.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "visits")
  public class Visit {
  
  @Column(name = "visit_date")
  private Date date;
  
  @NotEmpty
  @Column(name = "description")
  private String description;
  
  @ManyToOne
  @JoinColumn(name = "pet_id")
  private Pet pet;
  </code></pre>
  
  <p>}</p>
  
  <p>I have JPA Repositories for these Entities, PetRepository, OwnerRepository and VisitRepository.</p>
  
  <p>I have one @Service class.</p>
  
  <p><strong>OwnerServiceImpl.java</strong></p>
  
  <pre><code>@Override
  @Transactional
  public void updateOwnerPetVisits(int ownerId) throws DataAccessException {
  
  Owner owner = ownerRepository.findById(ownerId);
  
  int petId = owner.getPet().getPetId();
  
  Pet pet = findPetById(petId);
  
  Hibernate.initialize(pet.getVisits());
  
  OwnerResource ownerResource = new OwnerResource();
  ownerResource.setPet(pet);
  ownerResource.setPetVisits(pet.getVisits().size());
  
  }
  
  private Pet findPetById(int id) throws DataAccessException {
  return petRepository.findById(id);
  }
  </code></pre>
  
  <p>OwnerResource is a simple DTO.</p>
  
  <p><strong>OwnerResource.java</strong></p>
  
  <pre><code>public class OwnerResource {
   private Pet pet;
   private int petVisits
  }
  </code></pre>
  
  <p>Now I was trying to write the Integration Test for the OwnerServiceImpl#updateOwnerPetVisits.</p>
  
  <p><strong>OwnerServiceJPAIntegrationTests.java</strong></p>
  
  <pre><code>   @ContextConfiguration(locations = {"classpath:spring/spring-jpa.xml"})
     @RunWith(SpringJUnit4ClassRunner.class)
     public class OwnerServiceJPAIntegrationTests {
  
       @Autowired
       OwnerRepository ownerRepository;
  
       @Autowired
       PetRepository petRepository;
  
       @Autowired
       OwnerService ownerService;
  
  
       @Test
       public void testUpdateOwnerPetVisits(){
  
        Owner owner = createNewOwner();
  
        int petId = owner.getPet().getPetId();
  
        Pet pet = findPetById(petId);
  
        assertFalse(Hibernate.isInitialized(pet.getVisits());
  
        ownerService.updateOwnerPetVisits(owner.getOwnerId());
  
        assertTrue(Hibernate.isInitialized(pet.getVisits());
  
      }
  
  
  private Owner createNewOwner()  {
  
  Pet pet  = new Pet();
  pet.setBirthDate(today);
  petRepository.add(pet); // entityManager.persist();
  
  Visit visit1 = new Visit();
  visit1.setDate(today);
  visit1.setDescription("T");
  visit1.setPet(pet); 
  visitRepository.add(visit1); // entityManager.persist();
  
  Visit visit2 = new Visit();
  visit2.setDate(today);
  visit2.setDescription("P");
  visit2.setPet(pet); 
  visitRepository.add(visit2); // entityManager.persist();
  
  Owner owner = new Owner();
  owner.setPet(pet);
  ownerRepository.add(owner); // entityManager.persist();
  }
  </code></pre>
  
  <p>This works fine. </p>
  
  <p>Now if I <strong>remove the call, Hibernate.initialize(pet.getVisits());</strong> </p>
  
  <p>from OwnerServiceImpl#updateOwnerPetVisits. The <strong>test case should fail now but it still works</strong>.</p>