java 更新/删除查询不能输入 JPA

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28439141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:36:12  来源:igfitidea点击:

Update/delete queries cannot be typed JPA

javaspringjpaentitymanagersql-delete

提问by David Levesque

I'm using the Spring framework for working on training projects. I'm also using JPA and have been having a hell of a time. In my DAO implementation class, I've written:

我正在使用 Spring 框架来处理培训项目。我也在使用 JPA,并且玩得很开心。在我的 DAO 实现类中,我写了:

@Override
public void deleteEntries(Module mod) {
    System.out.println(mod.getDescription());
    entityManager.createQuery("delete from TrainingEntry e where e.module = :mod and e.completedDate IS NULL",
                              TrainingEntry.class).setParameter("mod", mod);

}

As you can see it's just a simple DELETE statement, but I get the error:

如您所见,这只是一个简单的 DELETE 语句,但我收到错误消息:

"Update/delete queries cannot be typed."

My guess is, that I need to use some of the EntityManagerclass methods to do the job, but I'm not certain, as there's next to nothing about this error online. I'm also having difficulty applying some solutions I've found online about JPA delete queries. Any point in the right direction or explanation as to why this error is thrown is much appreciated.

我的猜测是,我需要使用一些EntityManager类方法来完成这项工作,但我不确定,因为网上几乎没有关于此错误的信息。我也很难应用我在网上找到的有关 JPA 删除查询的一些解决方案。任何关于正确方向或解释为何抛出此错误的观点都非常感谢。

回答by meskobalazs

The declaration of the EntityManagermethods are the following:

EntityManager方法的声明如下:

Query createQuery(java.lang.String qlString)
<T> TypedQuery<T> createQuery(java.lang.String qlString, java.lang.Class<T> resultClass)
// The other three method is not relevant here

From this, you can clearly see, that you get a TypedQuery<T>because of the second parameter. If you remove it, you will get a simple Queryobject. That is what you need.

由此,您可以清楚地看到,TypedQuery<T>由于第二个参数,您得到了 a 。如果你删除它,你会得到一个简单的Query对象。这就是你所需要的。

回答by David Levesque

Try removing the TrainingEntry.classargument when calling createQuery(), since this is what makes it a "typed" query.

尝试TrainingEntry.class在调用 createQuery() 时删除参数,因为这使它成为“类型化”查询。

回答by Raja Bhowmick

entityManager.createQuery("delete from TrainingEntry e where e.module = :mod and e.completedDate IS NULL").setParameter("mod", mod);