Saving many objects in GAE with JPA

I am using JPA with Google App Engine. Let's say I have a very simple @Entity consisting of a key and a string, nothing more. Now I create 10,000 of these objects, put them in a list, and now I want to save them all.

If I try to use EntityManager em and a for loop to list all of my entities ...

for(MyEntity entity : listOfAllEntities) { em.persist(entity); } 

.. I will get an IllegalArgumentException:

java.lang.IllegalArgumentException: cannot work with several groups of objects in one transaction.

As I understand it, I need to close and reopen the EntityManager for each persist () call. Of course, this is very laborious. I try to run a task once a day, reloading all objects. According to GAE policy, a task has a timeout of 30 seconds.

Thus, the alternative is to save only 500 objects at a time and run the task several times, which, I think, is more complicated than it should be.

Is this the only way to achieve what I'm trying to do, or am I missing something here?

Solution: All answers point in one direction. I just created a one-to-many relationship by creating a dummy parent object. I really don't need a parent in my case, and that doesn't make much sense in the real world, so to speak. But after setting this dummy object as a parent for each of the child objects, I can save them in the same way as before, without worrying too much about transactions. Thanks to everyone.

+6
java google-app-engine jpa entity
source share
2 answers

This may be simplified, but you can simply add the X number of entities as children of the parent object, and this will result in them being in the same entity group. This will allow you to save them all in one transaction. Basically, create a one-to-many relationship between the parent and the children that are the objects you are trying to save.

+4
source share

This error says that you are using a transaction and trying to work with multiple groups of entities.

From the docs:

All data warehouse operations in a transaction must work with objects in the same entity group. This includes querying for objects by ancestor, retrieving objects by key, updating objects, and deleting objects. Please note that each root object belongs to a separate entity group, therefore, one transaction cannot create or work with several root objects.

You cannot just create more than one root (without a parent) object in a transaction, but you can do this if each of them is a child of another object. If you just want to create objects, do it outside of the transaction.

+1
source share

All Articles