public void testEmbeddedArray() {
Object id = null;
Key ownerKey = null;
try {
EmbeddedArrayOwner owner = new EmbeddedArrayOwner();
EmbeddedRelatedBase baseRel1 = new EmbeddedRelatedBase("First Base", 100);
EmbeddedRelatedSub subRel2 = new EmbeddedRelatedSub("Second Base", 200, "Other Type");
EmbeddedRelatedBase[] array = new EmbeddedRelatedBase[]{baseRel1, subRel2};
owner.setArray(array);
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 array", e);
fail("Exception occurred on persist of embedded array : " + 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("array.size"));
Object propVal = entity.getProperty("array.size");
assertNotNull(propVal);
long numChildren = (Long)entity.getProperty("array.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 array");
}
// Check retrieval
pm = pmf.getPersistenceManager();
try {
pm.currentTransaction().begin();
EmbeddedArrayOwner owner = (EmbeddedArrayOwner)pm.getObjectById(id);
EmbeddedRelatedBase[] array = owner.getArray();
assertEquals(2, array.length);
for (int i=0;i<array.length;i++) {
if (i == 0) {
assertTrue("First element incorrect",
array[i].getName().equals("First Base") && array[i].getValue() == 100 &&