ReferenceHandler implements ItemHandler { @Override public Object retrieve(MappedField mappedField, PersistenceAdapter adapter) { final ReferenceMapping mapping = (ReferenceMapping) mappedField.getMapping(); Object id = adapter.resolve(mapping.getPath()); LOGGER.debug("Path (resolved) from mapping is {} ({})", mapping.getPath(), id); if (mapping.getLookupMode() == LookupMode.Reference) { LOGGER.trace("Field {} is a reference, retrieving value of local field to resolve reference...", mappedField.getName()); final PersistenceResult result = adapter.getProperty(new ImmutablePersistenceRequest(mapping.getPath(), String.class, Mode.Relative)); if (result.hasResult() && (result.getValue() != null)) { id = result.getValue(); } else { id = MissingHandler.INSTANCE.retrieve(mappedField, adapter); } } LOGGER.trace("Retrieving reference to {} from {}", mappedField.getType(), id); try { return session.get(mapping.getImplementationType(), id); } catch (final PathNotFoundException e) { switch (mappedField.getMissingStrategy()) { case ReturnNull: return null; case DefaultValue: case ThrowException: default: throw new MissingException("Cannot find " + id + " while retrieving " + mappedField, e); } } } ReferenceHandler(Session session); @Override Object retrieve(MappedField mappedField, PersistenceAdapter adapter); @Override String toString();  }
@Test public void test() { final Session session = mock(Session.class); final PersistenceAdapter adapter = mock(PersistenceAdapter.class); when(adapter.resolve("/foo/bar")).thenReturn("/resolved/foo/bar"); final MappedField mappedField = new MappedFieldBuilder().withName("field").withType(EntityImplementingInterface.class) .withReferenceMapping("field", MyInterface.class, EntityImplementingInterface.class, "/foo/bar", LookupMode.Direct).create(); final ReferenceHandler handler = new ReferenceHandler(session); handler.retrieve(mappedField, adapter); verify(session).get(EntityImplementingInterface.class, "/resolved/foo/bar"); }