@Test
  public void addOneSideEntity_ClearCacheQueryForAllThenGetEntity_EntityGottenIsNowCached()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();
    RelationEntities.LazyManySide anyEntity = persistedOneSide.getManySides().get(0);

    Assert.assertEquals(1, persistedManySides.getCachedEntities().size());
    Assert.assertTrue(persistedManySides.getCachedEntities().containsValue(anyEntity));
  }
  @Test
  public void
      addOneSideEntity_ClearCacheQueryForAll_LazyCollectionRetrievedIdsButEntitiesNotCachedYet()
          throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");
    RelationEntities.LazyManySide manySide4 = new RelationEntities.LazyManySide("Many 4");
    RelationEntities.LazyManySide manySide5 = new RelationEntities.LazyManySide("Many 5");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);
    oneSide.getManySides().add(manySide4);
    oneSide.getManySides().add(manySide5);

    oneSideDao.clearObjectCache();
    manySideDao.clearObjectCache();

    RelationEntities.LazyOneSide persistedOneSide = oneSideDao.queryForId(oneSide.getId());
    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) persistedOneSide.getManySides();

    Assert.assertEquals(
        oneSide.getManySides().size(), persistedManySides.getRetrievedIndices().size());
    Assert.assertEquals(0, persistedManySides.getCachedEntities().size());
  }
  @Test
  public void removeEntity_RetrievedIdsAndCachedEntitiesDoNotContainThisEntityAnymore()
      throws Exception {
    RelationEntities.LazyOneSide oneSide = new RelationEntities.LazyOneSide("One");
    oneSideDao.create(oneSide);

    RelationEntities.LazyManySide manySide1 = new RelationEntities.LazyManySide("Many 1");
    RelationEntities.LazyManySide manySide2 = new RelationEntities.LazyManySide("Many 2");
    RelationEntities.LazyManySide manySide3 = new RelationEntities.LazyManySide("Many 3");

    oneSide.getManySides().add(manySide1);
    oneSide.getManySides().add(manySide2);
    oneSide.getManySides().add(manySide3);

    RelationEntities.LazyManySide removedEntity = manySide2;
    Long removedEntityId = removedEntity.getId();
    oneSide.getManySides().remove(removedEntity);

    OpenLazyLoadingEntitiesCollection persistedManySides =
        (OpenLazyLoadingEntitiesCollection) oneSide.getManySides();

    Assert.assertFalse(persistedManySides.getRetrievedIndices().contains(removedEntityId));
    Assert.assertFalse(persistedManySides.getCachedEntities().containsValue(removedEntity));
  }