简体   繁体   中英

Could any one tell me the reason of spring-data projection warning in this query?

I have the following query:

@Query("select c.status from Component c where c.id = ?1")
ComponentStatus findStatusByComponentId(ComponentId id);

Why IntelliJ is warning me with: 'Component' domain type or valid projection interface expected here?

Any one can help me?

Any Spring data repository method that takes the form " find.*By.+ " (eg " find<DOMAIN_TYPE>By<PROJECTION_INTERFACE> " or " findBy<PROJECTION_INTERFACE> ") is going to give that warning if it doesn't match a valid derived query. If you start typing "find" and wait for the autocomplete dialog box, it will suggest some of the derived query methods (or at least, the start of them).

IntelliJ 自动完成 Spring 数据派生查询 1

If you continue typing a valid domain type (in my case, RequestSampleTest) the autocomplete box will continue updating with more possibilities (there are a LOT of possible derived queries, depending on the Entity).

IntelliJ 自动完成 Spring 数据派生查询 2

This article ( https://www.baeldung.com/spring-data-derived-queries ) gives a really good introduction to Spring data derived queries. They're basically a streamlined way to perform basic search and sort queries.

As a note to your comment: By adding an 'x' in front of the method name, it no longer "looks like" a derived query, so the warning goes away.

Also of note: I think only IntelliJ Pro edition provides Spring support, so if you're not seeing those dropdowns, it might be because you're on Community Edition.

Probably because it go against the pattern that Spring Data is expects. And you can do that query (something) like this:

ComponentStatus findStatusById(ComponentId id);

You don't need to write the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM