用于延迟加载集合的Hibernate.initialized的Spring集成测试

时间:2017-12-13 15:56:42

标签: hibernate integration-testing spring-test

我有一个延迟加载的@OneToMany关系,我已经使用Hibernate.initialize()进行了初始化。这是我的实体。

Pet.java

@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<Visit> visits;

}

Visit.java

@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;

}

我有这些实体的JPA存储库,PetRepository,OwnerRepository和VisitRepository。

我有一个@Service类。

OwnerServiceImpl.java

@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);
}

OwnerResource是一个简单的DTO。

OwnerResource.java

public class OwnerResource {
 private Pet pet;
 private int petVisits
}

现在我正在尝试为OwnerServiceImpl#updateOwnerPetVisits编写集成测试。

OwnerServiceJPAIntegrationTests.java

   @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();
}

这很好用。

现在如果我删除了这个电话,Hibernate.initialize(pet.getVisits());

来自OwnerServiceImpl#updateOwnerPetVisits的

测试用例现在应该失败但仍然有效

1 个答案:

答案 0 :(得分:1)

您正在createNewOwner()中创建实体,那么它们为什么要延迟加载?

如果要模拟延迟加载,必须先清除EntityManger持久化上下文,然后再调用findPetById()

 @Test
 public void testUpdateOwnerPetVisits(){

  Owner owner = createNewOwner();

  int petId = owner.getPet().getPetId();

  // Clears the persistence context
  entityManager.clear();

  Pet pet = findPetById(petId);

  assertFalse(Hibernate.isInitialized(pet.getVisits());

  ownerService.updateOwnerPetVisits(owner.getOwnerId());

  assertTrue(Hibernate.isInitialized(pet.getVisits());

}