Skip to content

Commit

Permalink
#80
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhrobbins committed Jan 10, 2014
1 parent 4df8f2e commit f5b6cec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ private void initializeField(Object entity, String field) throws IllegalAccessEx

PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();

// if the requested 'field' exists and hasn't been
if ( (PropertyUtils.isReadable(entity, field)) && (!unitUtil.isLoaded(entity, field)) ) {
Object value = PropertyUtils.getProperty(entity, field);
// is the 'field' a collection?
if (value instanceof Collection<?>) {
// this is why we are here!
// initialize the collection
initializeCollection((Collection<?>) value);
}
}
Expand Down Expand Up @@ -77,6 +81,7 @@ private void initializeCollection(Collection<?> collection) {
if(collection == null) {
return;
}
// we force the ORM to initialize the collection by touching it (hasNext)
collection.iterator().hasNext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mock;
import org.robbins.flashcards.dto.FlashCardDto;
import org.robbins.flashcards.dto.TagDto;
import org.robbins.flashcards.exceptions.ServiceException;
import org.robbins.tests.BaseMockingTest;
Expand All @@ -36,15 +37,17 @@ public class HibernateFieldMapperUT extends BaseMockingTest {
private FieldInitializerUtil fieldInitializerUtil;

private TagDto tagDto;

private String FIELD_NAME = "name";
private String COLLECTION_FIELD_NAME = "flashcards";

@Before
public void before() {
String TAG_NAME = "test_tag";
tagDto = new TagDto(TAG_NAME);
fieldInitializerUtil = new FieldInitializerUtil();

tagDto = new TagDto();
tagDto.setFlashcards(new HashSet<FlashCardDto>());

when(mockEntityManager.getEntityManagerFactory()).thenReturn(mockEntityManagerFactory);
when(mockEntityManagerFactory.getPersistenceUnitUtil()).thenReturn(mockPersistenceUnitUtil);
when(mockEntityManagerFactory.createEntityManager()).thenReturn(mockEntityManager);
Expand All @@ -54,7 +57,7 @@ public void before() {

@Test
public void initializeEntity() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, ServiceException {
fieldInitializerUtil.initializeEntity(tagDto, new HashSet<String>(Arrays.asList(FIELD_NAME)));
fieldInitializerUtil.initializeEntity(tagDto, new HashSet<String>(Arrays.asList(COLLECTION_FIELD_NAME)));
}

@Test
Expand Down

0 comments on commit f5b6cec

Please sign in to comment.