我正在尝试从 GAE 应用程序内的数据存储中删除特定类型的所有实体。我有以下行:
em.createQuery("DELETE m FROM "+UpdateMessage.class.getSimpleName()+" m").executeUpdate();
我看到以下异常:
Unable to update most recent message in datatstore: Candidate class could not be found: DELETE
我假设我没有正确使用别名,因为它将 DELETE 误认为是实际的类。我尝试在没有别名的情况下执行 DELETE FROM MyClassType
,但这似乎不起作用。
有什么想法吗?
最佳答案
如果您想删除所有实体,则不需要变量,如此处所述 [1]。
此外,您正在使用 getSimpleName() 方法,我对 JPA 了解甚少,但我见过的所有代码片段都使用 getName() 方法。请参阅此处的差异 [2]。因此,查询将是:
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?
关于java - 删除特定类型的所有实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28756079/