views:

61

answers:

3

How to store complex objects? I have an object in its list of child objects in the child object has four list of child objects when calling makePersistent (person) object is not saved. Help!!!!!!!!!!!!

I am call pm.makePersistent(); but Lists

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

    @Persistent
    private List<PleasantElementEntity> pleasantElements = new ArrayList<PleasantElementEntity>();

    @Persistent
    private List<UnpleasantElementEntity> unpleasantElements = new ArrayList<UnpleasantElementEntity>();

    @Persistent
    private List<SetViewElementEntity> setViewElements = new ArrayList<SetViewElementEntity>();

not saved!!!

example:

@PersistenceCapable(table = "persons", identityType = IdentityType.APPLICATION)
public class PersonEntity {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;   

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

@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>();

    @Persistent
    private List<PleasantElementEntity> pleasantElements = new ArrayList<PleasantElementEntity>();

    @Persistent
    private List<UnpleasantElementEntity> unpleasantElements = new ArrayList<UnpleasantElementEntity>();

    @Persistent
    private List<SetViewElementEntity> setViewElements = new ArrayList<SetViewElementEntity>();
}


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

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}

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

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}

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

    @Persistent
    private String code;

    @Persistent
    private Text cmComments;
}
+1  A: 
  • Check you declared all of the classes in the object hierarchy as @PersistenceCapable
  • Check you declared the list/set object variables as per google documentation? ie:
    @Element(dependent = "true") 
    private Set tags = new HashSet(); 
    
  • Have you tried using makePersistentAll(person)?
  • Check your data is really being saved viewing your data at http://localhost:8888/_ah/admin/
  • Are you closing your Persistence handler? ie pm.close(). Data is never persisted until you close the handler, ie:
    // All objects are manupliated using a pm object
    PersitenceManager pm = PMF.instance().getPersistenceManager();
    // do work
    // Store the changes
    pm.close()
    
Jacob
why @Element(dependent = "true") ??
Artem
strust object --> class person {list<Offers> ls;} , class Offers{ List<Object1> o1;List<Object2> o2;List<Object2> o2;}
Artem
@Element?.... See the documentation on child parent relationships: http://code.google.com/appengine/docs/java/datastore/relationships.html
Jacob
add @Element, not saved!
Artem
yes, closed finally { if (null != pm) { pm.close(); }
Artem
A: 

It may be a problem in the relationship between the objects? Try done manually save each object from complex objects

Nikolay Moskvin
If you save a child object, when you persist the parent you will get an exception, unless you make the objects part of separate entity sets.
Jacob
Yes, you right!
Nikolay Moskvin
A: 

It appears that you have not properly activated generator primary keys in the JDO. This is not a error of the JDO ?

I found it:

"There is currently a bug preventing owned one-to-many relationships where the parent and the child are the same class, making it difficult to model tree structures. This will be fixed in a future release. You can work around this by storing explicit Key values for either the parent or children."

http://code.google.com/intl/en-EN/appengine/docs/java/datastore/usingjdo.html

I could not wait for the fix, so I have already worked around this issue by having all my entities of this type:

  • having the same entity group parent (of a different class)
  • using an ArrayList<[Entity]> of key values to store each entity's "children"
  • using a key value to store each entity's "parent" (null for the root instance, even though it still has a entity group parent).
Nikolay Moskvin