在 STS 3.1 的 Spring Data JpaRepository 接口中到处都是“无效的派生查询”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13072502/
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-09-08 05:28:03  来源:igfitidea点击:

Get "Invalid derived query" error all over the place in our Spring Data JpaRepository interfaces in STS 3.1

springspring-mvcsts-springsourcetoolsuitespring-dataspring-data-jpa

提问by skel625

We have implemented our repositories exactly as demonstrated in the Spring Data documentation. Everything was fine until we upgraded from STS 2.9 to STS 3.1. All attempts to get these errors to disappear have failed, and in some cases they don't even make sense! They don't match any properties in either the interface or the entities used!

我们完全按照 Spring Data 文档中的说明实现了我们的存储库。一切都很好,直到我们从 STS 2.9 升级到 STS 3.1。使这些错误消失的所有尝试都失败了,在某些情况下它们甚至没有意义!它们与界面或使用的实体中的任何属性都不匹配!

Here is an example:

下面是一个例子:

public interface CreditNotesRepository extends JpaRepository<CreditNotes, Long> {

    CreditNotes findCurrentCreditNotes(Long shipmentDetailId);
}

The findCurrentCreditNotesis a named query in our entity. This code executes perfectly fine.

findCurrentCreditNotes是我们实体中的命名查询。这段代码执行得非常好。

@NamedQueries({
    @NamedQuery(name = "CreditNotes.getCount", query = "SELECT COUNT(f) FROM CreditNotes f"),
    @NamedQuery(name = "CreditNotes.findCurrentCreditNotes", query =
        "SELECT creditNotes FROM CreditNotes creditNotes"
        + " WHERE creditNotes.shipmentDetail.shipmentDetailId = ?1 "
        + " AND creditNotes.notesSeqNumber =  (SELECT max(creditNotes2.notesSeqNumber) FROM CreditNotes creditNotes2"
        + " WHERE creditNotes.shipmentDetail.shipmentDetailId = creditNotes2.shipmentDetail.shipmentDetailId)")
})

And the error we get:

我们得到的错误:

Invalid derived query! No property find found for type ca.cole.freight.model.CreditNotes

Although this is just a flag (doesn't affect compilation), it is annoying and confusing. Can anyone shed some light on this? And explain it to me like I'm 6 years old! ;)

虽然这只是一个标志(不影响编译),但它很烦人和令人困惑。任何人都可以对此有所了解吗?并像我 6 岁一样向我解释!;)

回答by Tuan

At the poston the Spring Forum, Spring Team announced that

岗位上的春季论坛,Spring团队宣布,

It is already fixed for STS 3.3.0

STS 3.3.0 已经修复

I didn't check this version yet. But I'm using 3.5.0.RELEASE and the problem comes back! My fix is to uncheck Invalid Derived Query

我还没有检查这个版本。但是我正在使用 3.5.0.RELEASE 并且问题又回来了!我的解决方法是取消选中Invalid Derived Query

Invalid Derived Query

无效的派生查询

回答by Grubhart

It's an IDE error explained in the following post:

这是以下帖子中解释的 IDE 错误:

http://forum.springsource.org/showthread.php?138585-Invalid-derived-query!-No-property-delete-found-for-type-java-lang-Object

http://forum.springsource.org/showthread.php?138585-Invalid-derived-query!-No-property-delete-found-for-type-java-lang-Object

In the meantime, you can turn off the validation in preferences/spring/project validators/Data validatoruncheck invalid derived queryand STS wont throw the marker anymore.

同时,您可以关闭首选项/弹簧/项目验证器/数据验证器中的验证,取消选中无效的派生查询,STS 不会再抛出标记。

回答by Pawe? Dul?ba

There is also workaround for this. Add @Queryannotation on your method definition in Your repository without JPQL/SQL query defined.

也有解决方法。@Query在您的存储库中为您的方法定义添加注释,而未定义 JPQL/SQL 查询。

Here is example :

这是示例:

@Query
List<OwnerModel> findByFirstNameAndAgeNotZero(@Param(value = "firstName") String firstName);

In this case named query OrderModel.findByFirstNameAndAgeNotZerowill be used. Your Eclipse error Invalid derived queryshould also disappear without need of disabling validation as described by @Tuan Dang

在这种情况下,OrderModel.findByFirstNameAndAgeNotZero将使用命名查询。您的 Eclipse 错误Invalid derived query也应该消失,而无需禁用@Tuan Dang 所述的验证

Checked on Eclipse 4.5.1 with Spring plugin installed for @NamedQueryand @NamedNativeQuery.

在 Eclipse 4.5.1 上检查并为@NamedQuery和安装了 Spring 插件@NamedNativeQuery

回答by Apropos

I've just been going through this myself. Unfortunately, the implementation of Spring Data changed between 1.1 and 1.2. It no longer supports the <repository>XML declaration. You can set up a custom postfix, but by default, it expects a bean of class name <InterfaceName>Impl. If it can't find the custom repository implementation, you start getting errors like the one you're encountering. It's trying to create methods to query for objects based on names of methods in your interface.

我自己刚刚经历过这个。不幸的是,Spring Data 的实现在 1.1 和 1.2 之间发生了变化。它不再支持<repository>XML 声明。您可以设置自定义后缀,但默认情况下,它需要一个 class name 的 bean <InterfaceName>Impl。如果它找不到自定义存储库实现,您就会开始收到与您遇到的类似的错误。它试图创建方法来根据界面中的方法名称查询对象。

An alternative is to back your Spring Data version down to 1.1 and specify a schemalocation of http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsdin your XML.

另一种方法是将您的 Spring Data 版本降低到 1.1 并http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd在您的 XML 中指定架构位置。