0
votes

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.

4

4 Answers

1
votes

This section of the docs deals with exactly that.

1
votes

i did this:

public static final Key root_key = KeyFactory.createKey("Object", "RootKey");

...

so a typical datastore persistent object will set the id in the constructor instead of getting one automatically

public DSO_MyType(string Name, Key parent)
    {
        KeyFactory.Builder b = new KeyFactory.Builder(parent);;
        id = b.addChild(DSO_MyType.class.getSimpleName() , Name).getKey();
    }

and you pass root_key as the parent

i'm not sure if you can pass different parents to objects of the same kind

0
votes

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.

0
votes

Gopi, AFAIK you don't have to do that... this should work (haven't tested it):

List<User> userList = new ArrayList<User>();
userList.add(new User("a"));
userList.add(new User("b"));
userList.add(new User("c"));
UserDAO().getInstance().addObjects(userList);

Again, AFAIK, this should put all these objects in the same entity group. I'd love to know if I am wrong.