Skip to content

Commit 2298c1f

Browse files
Merge pull request #4697 from MicrosoftDocs/copilot/warn-optional-query-compatibility
Add warning: @query methods cannot return Optional<T>
2 parents d7c5712 + 373f0c8 commit 2298c1f

1 file changed

Lines changed: 3 additions & 0 deletions

File tree

articles/cosmos-db/quickstart-java.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ public interface ItemRepository extends CosmosRepository<Item, String> {
235235
}
236236
```
237237

238+
> [!IMPORTANT]
239+
> `@Query`-annotated methods must return `List<T>` or a single `T`, not `Optional<T>`. `Optional<T>` is only supported for derived query methods such as `findById`. Using `Optional<T>` with `@Query` compiles without errors but causes a `ClassCastException` at runtime.
240+
238241
Fetch all of the results of the query using `repository.getItemsByCategory`. Loop through the results of the query.
239242

240243
```java

0 commit comments

Comments
 (0)