surinder-insonix / datanucleus-appengine

Automatically exported from code.google.com/p/datanucleus-appengine
0 stars 0 forks source link

One to many relationship problem #219

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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();
    }
}

hen, 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.

Original issue reported on code.google.com by kanic.a...@gmail.com on 11 Aug 2010 at 12:08

GoogleCodeExporter commented 8 years ago
Somebody!!! 
I really need your help. 
Thanks.

Original comment by kanic.a...@gmail.com on 13 Aug 2010 at 11:57

GoogleCodeExporter commented 8 years ago
A few things:
1) Does this ever happen locally?
2) Looking at the output of the log line isn't a good indication of whether or 
not the child object is being saved since sometimes the child gets flushed 
immediately and sometimes it gets flushed when the pm is closed.  You can only 
count on objects being persistent after you've closed the pm (or after you've 
committed the txn, if you're using txns).  Do you have some other indication 
that the child object is not being saved?
3) The call to pm.makePersistent(dbOffer) isn't necessary - the object is 
already persistent and all changes to that object should be "seen" by the 
datastore.
4) You should really be using a list-ordering on your 1-Many relationships:
http://code.google.com/appengine/docs/java/datastore/relationships.html#Owned_On
e_to_Many_Relationships

Original comment by max.r...@gmail.com on 16 Aug 2010 at 7:32

GoogleCodeExporter commented 8 years ago
Max, many thanks.

My comments below:
1) Does this ever happen locally?
No, I can not reproduce this issue on my locally machine.
2) Do you have some other indication that the child object is not being saved?
Yes. We not see new child object in Datastore Viewer.
3) The call to pm.makePersistent(dbOffer) isn't necessary.
Thanks. I not catch this information.
4) You should really be using a list-ordering on your 1-Many relationships.
Ok. I will try to test this case. But in really we are not in need of ordering.
5) What SDK you are using?
We started with SDK 1.3.1. But now we are using SDK 1.3.5 with datanucleus 
version 1.0.7

Original comment by kanic.a...@gmail.com on 17 Aug 2010 at 3:55

GoogleCodeExporter commented 8 years ago
gracias por el aporte, me sirvió de mucho. gracias por el ejemplo 1:N

Original comment by d...@aconsultores.com.mx on 28 Jul 2011 at 3:45

GoogleCodeExporter commented 8 years ago

Original comment by googleco...@yahoo.co.uk on 21 Sep 2011 at 3:51

GoogleCodeExporter commented 8 years ago
Can't reproduce this. Provide a testcase that demonstrates something and it 
could be moved forward

Original comment by googleco...@yahoo.co.uk on 1 Nov 2011 at 4:18