javagoogle-app-enginejdoentity-groupsentity-group-transactions

How to create entities in one Entity group?


I am building an app based on google app engine (Java) using JDO for persistence.

Can someone give me an example or a point me to some code which shows persisting of multiple entities (of same type) using javax.jdo.PersistenceManager.makePersistentAll() within a transaction.

Basically I need to understand how to put multiple entites in one Entity Group so that they can be saved using makePersistentAll() inside transaction.


Solution

  • Thanks for the response Nick.

    This document only tells about implicit handling of entity groups by app engine when its a parent-child relationship. I want to save multiple objects of same type using PeristentManager.makePersistentAll(list) within a transaction. If objects are not same Entity Group this throws exception. Currently I could do it as below but think there must be a better and more appropriate approach to do this -

    User u1 = new User("a");
    UserDAO.getInstance().addObject(user1); 
    // UserDAO.addObject uses PersistentManager.makePersistent() in transaction and user 
    // object now has its Key set. I want to avoid this step.
    
    User u2 = new User("x"); 
    u2.setKey(KeyFactory.createKey(u1.getKey(),User.class.getSimpleName(), 100 /*some random id*/)); 
    
    User u3 = new User("p");
    u3.setKey(KeyFactory.createKey(u1.getKey(), User.class.getSimpleName(), 200)); 
    
    UserDAO.getInstance().addObjects(Arrays.asList(new User[]{u2, u3})); 
    // UserDAO.addObjects uses PersistentManager.makePersistentAll() in transaction.
    

    Although this approach works, the problem with this is that you have to depend on an already persistent entity to create an entity group.