Hi Aleksey,

There are a few things you can try including using transactions
http://code.google.com/intl/de-DE/appengine/docs/java/datastore/transactions.html
.
But from personal experience I have to warn you to be careful with one-
to-many in JDO and recommend you don't use these relationships right
now, as you might eventually run into very strange behaviour, e.g. if
you ever have to remove children. Refer to these issues if you should
experience any strange behaviour: 
http://code.google.com/p/datanucleus-appengine/issues/list
.

As an alternative store the child entities in the entity group of the
parent explicitly by using setParentKey and a field like
@Persistent
@Extension(vendorName="datanucleus", key="gae.parent-pk",
value="true")
private Key parentKey;

You can then find all children with a query on the child type and
parent key and manage this kind of relationship yourself.

Hope this helps :-)


On 13 Aug., 14:01, Aleksey Kiselev <[email protected]> wrote:
> 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to