Skip to content

Commit

Permalink
Merge pull request #97 from justinhrobbins/feature_80
Browse files Browse the repository at this point in the history
Feature 80
  • Loading branch information
justinhrobbins committed Jan 11, 2014
2 parents 474d57f + 4489bfd commit f4fe54c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 2 additions & 1 deletion FlashCards_Domain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<name>FlashCards_Domain</name>
<description>FlashCards_Domain description</description>
<properties>
<skip.coveralls>false</skip.coveralls>
<skip.coveralls>true</skip.coveralls>
<skip.cobertura>true</skip.cobertura>
</properties>
<scm>
<connection>scm:git:git@github.com:justinhrobbins/FlashCards_App.git</connection>
Expand Down
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 f4fe54c

Please sign in to comment.