Search code examples
javagoogle-app-enginejpql

Deleting all Entities of a certain type


I am trying to delete all entities of a certain type from the datastore within my GAE application. I have the following line:

em.createQuery("DELETE m FROM "+UpdateMessage.class.getSimpleName()+" m").executeUpdate();  

I am seeing the following exception:

 Unable to update most recent message in datatstore: Candidate class could not be found: DELETE 

I'm assuming that I am not doing the aliasing right, as it is mistaking the DELETE for an actual class. I tried just doing DELETE FROM MyClassType without the alias, but that didn't seem to work.

Any ideas?


Solution

  • If you want to remove all entities you won't need a variable as explained here [1].

    Also, you are using the method getSimpleName(), I know little about JPA but all of the code snippets I've seen use the getName() method instead. See differences here [2]. Therefore, the query would be:

    em.createQuery("DELETE FROM " + UpdateMessage.class.getName()).executeUpdate();  
    

    [1] http://www.objectdb.com/java/jpa/query/jpql/delete

    [2] What is the difference between canonical name, simple name and class name in Java Class?