Hi, group!!!

Sometimes my GAE application can't persist collection of child
elements.
I have following objects structure.
@PersistenceCapable(table = "persons", identityType =
IdentityType.APPLICATION)
public class PersonEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    @Persistent
    private String name;

    @Persistent
    private List<OfferEntity> offers = new ArrayList<OfferEntity>();

    // ... getter and setter
}

@PersistenceCapable(table = "offers", identityType =
IdentityType.APPLICATION)
public class OfferEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;

    @Persistent
    private List<ChoosedElementEntity> choosedElements = new
ArrayList<ChoosedElementEntity>();

    // ... getter and setter
}

@PersistenceCapable(table = "offer_selections_ce", identityType =
IdentityType.APPLICATION)
public class ChoosedElementEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private com.google.appengine.api.datastore.Key id;

    @Persistent
    private String filed;

    // ... getter and setter
}

First of all I try to persist object of class PersonEntity. It is all
right.
PersistenceManager = null;
try {
    pm = getPersistenceManager();
    PersonEntity person = new PersonEntity();
    //  set person fields
    pm.makePersistent(person);
} catch (Throwable th) {
} finally {
    if (null != pm) {
        pm.close();
    }
}

Then, I try to add some child elements to OfferEntity. And I am
thoroughly confused: Sometimes child object persists and sometimes
not. This behavior is very strange.
String offerId = ...;
PersistenceManager pm = null;
try {
    pm = getPersistenceManager();
    OfferEntity dbOffer = pm.getObjectById(OfferEntity.class,
KeyFactory.stringToKey(offerId));
    if (null != dbOffer) {
        ChoosedElementEntity element = new ChoosedElementEntity();
        // set element fields
        dbOffer.getChoosedElements().add(element);
        log.log(Level.INFO, "try to commit 'added choosed elements':
offerId=" + dbOffer.getId() + ", size: " +
dbOffer.getChoosedElements().size() + ", elements: " +
dbOffer.getChoosedElements());
        pm.makePersistent(dbOffer);
    }
} catch (Throwable th) {
    log.log(Level.SEVERE, "Can't add choosed elements to offer entity:
offerId=" + offerId, th);
    throw new ServiceException("Can't add choosed elements to offer
entity: offerId=" + offerId, th);
} finally {
    if (null != pm) {
        pm.close();
    }
}

I try to analyze some child information before persist parent object
and I note, that from time to time child element has/hasn't value for
primary key.
Example:
"try to commit 'added choosed elements': offerId=persons(1092001)/
offers(1), size: 1, elements:
[ChoosedElementEntity{id=persons(1092001)/offers(1)/
offer_selections_ce(5001), filed='filed765'}]"
or
"try to commit 'added choosed elements': offerId=persons(1092001)/
offers(1), size: 1, elements:  [ChoosedElementEntity{id=null,
filed='filed766'}]"

Any help would be greatly appreciated. Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to