public void testEmbeddedCollection() {
Object id = null;
Key ownerKey = null;
try {
EmbeddedCollectionOwner owner = new EmbeddedCollectionOwner();
EmbeddedRelatedBase baseRel1 = new EmbeddedRelatedBase("First Base", 100);
owner.addChild(baseRel1);
EmbeddedRelatedSub subRel2 = new EmbeddedRelatedSub("Second Base", 200, "Other Type");
owner.addChild(subRel2);
pm.currentTransaction().begin();
pm.makePersistent(owner);
pm.currentTransaction().commit();
id = pm.getObjectId(owner);
ownerKey = owner.getKey();
} catch (Exception e) {
NucleusLogger.PERSISTENCE.error("Exception on persist of embedded collection", e);
fail("Exception occurred on persist of embedded collection : " + e.getMessage());
} finally {
if (pm.currentTransaction().isActive()) {
pm.currentTransaction().rollback();
}
pm.close();
}
pmf.getDataStoreCache().evictAll();
// Check datastore values direct
try {
Entity entity = ds.get(ownerKey);
assertTrue(entity.hasProperty("children.size"));
Object propVal = entity.getProperty("children.size");
assertNotNull(propVal);
long numChildren = (Long)entity.getProperty("children.size");
assertEquals(2, numChildren);
assertTrue(entity.hasProperty("name.0"));
assertTrue(entity.hasProperty("value.0"));
assertTrue(entity.hasProperty("name.1"));
assertTrue(entity.hasProperty("value.1"));
} catch (EntityNotFoundException enfe) {
fail("Failure to retrieve Entity for persisted owner with embedded collection");
}
// Check retrieval
pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
EmbeddedCollectionOwner owner = (EmbeddedCollectionOwner)pm.getObjectById(id);
Collection<EmbeddedRelatedBase> children = owner.getChildren();
assertEquals(2, children.size());
boolean firstPresent = false;
boolean secondPresent = false;
for (EmbeddedRelatedBase elem : children) {
if (elem.getName().equals("First Base") && elem.getValue() == 100 &&