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.
java google-app-engine jpa entity
pgruetter
source share