Skip to content

Commit

Permalink
Using the identity map for any ids that exist within.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrentharris committed Jan 22, 2016
1 parent 85980e0 commit 720e111
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Expand Up @@ -16,7 +16,7 @@ public NotFoundException(Class<?> type, long id) {
}

public NotFoundException(Class<?> type, Collection<Long> ids) {
super(type.getSimpleName() + "id in (" + String.join(", ", (String[]) ids.stream().map(id -> String.valueOf(id)).toArray()) + ") not found");
super(type.getSimpleName() + " id in (" + String.join(", ", (String[]) ids.stream().map(id -> String.valueOf(id)).toArray()) + ") not found");
}

}
19 changes: 12 additions & 7 deletions domain/src/main/java/joist/domain/uow/UnitOfWork.java
Expand Up @@ -130,18 +130,23 @@ <T extends DomainObject> T load(Class<T> type, Long id) {
}

<T extends DomainObject> List<T> load(Class<T> type, Collection<Long> ids) {
boolean containsAllIds = this.identityMap.getIdsOf(type).containsAll(ids);
if (containsAllIds) {
List<T> loaded = new ArrayList<T>();
for (Long id : ids) {
loaded.add((T) this.identityMap.findOrNull(type, id));
List<Long> missingIds = new ArrayList<Long>();
List<T> loaded = new ArrayList<T>();
for (Long id : ids) {
T obj = (T) this.identityMap.findOrNull(type, id);
if (obj == null) {
missingIds.add(id);
} else {
loaded.add(obj);
}
return loaded;
}

Alias<T> a = AliasRegistry.get(type);
try {
return Select.from(a).where(a.getIdColumn().in(ids)).list();
if (!missingIds.isEmpty()) {
loaded.addAll(Select.from(a).where(a.getIdColumn().in(missingIds)).list());
}
return loaded;
} catch (NotFoundException nfe) {
throw new NotFoundException(type, ids);
}
Expand Down

0 comments on commit 720e111

Please sign in to comment.