I would like to initialize my local data store with some data using a
regular Java program (I do not want to start the Development Server
and call a service/servlet), and I'm getting the following exception

»»» EXCEPTION «««
Exception in thread "main" java.lang.NullPointerException: No API
environment is registered for this thread.
        at
com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:
70)
        at
com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:
80)
        at com.google.appengine.api.datastore.Query.<init>(Query.java:84)
        at
org.datanucleus.store.appengine.query.DatastoreQuery.validate(DatastoreQuery.java:
649)
        at
org.datanucleus.store.appengine.query.DatastoreQuery.performExecute(DatastoreQuery.java:
215)
        at
org.datanucleus.store.appengine.query.JPQLQuery.performExecute(JPQLQuery.java:
77)
        at org.datanucleus.store.query.Query.executeQuery(Query.java:1489)
        at org.datanucleus.store.query.Query.executeWithMap(Query.java:1398)
        at org.datanucleus.jpa.JPAQuery.getResultList(JPAQuery.java:163)
        at cyberRabbit.billingSite.server.dao.PersonDAO.clean(PersonDAO.java:
66)
        at
cyberRabbit.billingSite.test.InitializeDatabase.cleanDatastore(InitializeDatabase.java:
36)
        at
cyberRabbit.billingSite.test.InitializeDatabase.initDatastore(InitializeDatabase.java:
22)
        at
cyberRabbit.billingSite.test.InitializeDatabase.main(InitializeDatabase.java:
18)
«««


»»» CODE «««
public class InitializeDatabase {
        public static void main(String[] args) {
                new InitializeDatabase().initDatastore();
        }

        public void initDatastore() {
                cleanDatastore();
                (...)
        }

        private static void cleanDatastore() {
                try {
                        // No transaction
                        PersonDAO.clean();
                } finally {
                        if (EMF.getEntityManager().getTransaction().isActive()) 
{
                                System.out.println("RB!!!");
                                
EMF.getEntityManager().getTransaction().rollback();
                        }
                        EMF.close();
                }
        }
}


public class PersonDAO {
(...)
        public static void clean() {
                EntityManager em = EMF.getEntityManager();
                Query query = em.createQuery("SELECT p FROM Person p");

                for (Object personObj : query.getResultList()) {
                        Person person = (Person)personObj;
                        em.remove(person);
                }
        }
(...)
}

public final class EMF {
        private static final EntityManagerFactory emfInstance = Persistence
                        .createEntityManagerFactory("transactions-optional");

        private static ThreadLocal<EntityManager> threadLocal = new
ThreadLocal<EntityManager>();

        private EMF() {
        }

        public static EntityManager getEntityManager() {
                if (threadLocal.get() == null) {
                        threadLocal.set(emfInstance.createEntityManager());
                }
                return threadLocal.get();
        }

        public static void close() {
                EntityManager entityManager = getEntityManager();
                entityManager.close();
                threadLocal.set(null);
        }

«««

Is there a way to do this? How?

Thanks in advance
António Casqueiro

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to