ForeignKeyListHolder { public List<U> get() { if (this.loaded == null) { if (this.parent.isNew() || UnitTesting.isEnabled()) { this.loaded = new ArrayList<U>(); } else { if (!UoW.isOpen()) { throw new DisconnectedException(); } if (!EagerLoading.isEnabled()) { Select<U> q = Select.from(this.childAlias); q.where(this.childForeignKeyToParentColumn.eq(this.parent)); q.orderBy(this.childAlias.getIdColumn().asc()); q.limit(UoW.getIdentityMap().getCurrentSizeLimit()); this.loaded = q.list(); } else { MapToList<Long, U> byParentId = UoW.getEagerCache().get(this.childForeignKeyToParentColumn); if (!byParentId.containsKey(this.parent.getId())) { Collection<Long> idsToLoad = UoW.getEagerCache().getIdsToLoad(this.parent, this.childForeignKeyToParentColumn); if (!idsToLoad.contains(this.parent.getId())) { throw new IllegalStateException("Instance has been disconnected from the UoW: " + this.parent); } this.eagerlyLoad(byParentId, idsToLoad); } this.loaded = new ArrayList<U>(); this.loaded.addAll(byParentId.get(this.parent.getId())); } } if (this.addedBeforeLoaded.size() > 0 || this.removedBeforeLoaded.size() > 0) { this.loaded.addAll(this.addedBeforeLoaded); this.loaded.removeAll(this.removedBeforeLoaded); this.loaded = Copy.unique(this.loaded); } this.proxy = new ListProxy<U>(this.loaded, this.listDelegate); } return this.proxy; } ForeignKeyListHolder(
    T parent,
    Alias<U> childAlias,
    ForeignKeyAliasColumn<U, T> childForeignKeyToParentColumn,
    ListProxy.Delegate<U> listDelegate); List<U> get(); void add(U instance); void remove(U instance); void set(List<U> instances); @Override String toString();  }
@Test public void testEagerlyLoadOutsideUoWWithNoIdIsOkay() { DummyDomainObject parent = new DummyDomainObject(); ForeignKeyListHolder<DummyDomainObject, DummyDomainObject> h = new ForeignKeyListHolder<DummyDomainObject, DummyDomainObject>(parent, null, null, null); Assert.assertEquals(0, h.get().size()); }
@Test public void testEagerlyLoadOutsideUoWWithNewIdIsOkay() { DummyDomainObject parent = new DummyDomainObject(); ForeignKeyListHolder<DummyDomainObject, DummyDomainObject> h = new ForeignKeyListHolder<DummyDomainObject, DummyDomainObject>(parent, null, null, null); parent.setId(1l); Assert.assertEquals(0, h.get().size()); }
@Test public void testEagerlyLoadOutsideUoWFails() { DummyDomainObject parent = new DummyDomainObject(); ForeignKeyListHolder<DummyDomainObject, DummyDomainObject> h = new ForeignKeyListHolder<DummyDomainObject, DummyDomainObject>(parent, null, null, null); parent.setId(1l); parent.getChanged().clear(); try { h.get(); Assert.fail(); } catch (RuntimeException e) { Assert.assertEquals("The UoW is currently closed", e.getMessage()); } }