CodeLearn
CodeLearn

Reputation: 37

trying to use "OR" and "AND" in the same JPA method signature

findByFloorIdAndCategoryOrAreaIdAndCategory

I need to query with ((flooId and category) or (areaId and category)) from the same table. The above given name is not working. It's throwing the following error.

List<SpaceToDevice> spaceToDevices = spaceToDeviceRepository.findByFloorIdAndCategoryOrAreaIdAndCategory(id, category, id, category);

Error: with root causecom.azure.data.cosmos.CosmosClientException: 
    Gateway Failed to Retrieve Query Plan: 
    Message: {"Errors":["Invalid query. Specified duplicate parameter name '@category'."]}

Upvotes: 0

Views: 198

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81862

While there might be a bug hiding you really shouldn't do this.

Query derivation intentionally doesn't offer any way to control the precedence between AND and OR operation. The idea behind query derivation is to create queries for simple method names where the SQL/JPQL is obvious from the method name alone. But names like findByFloorIdAndCategoryOrAreaIdAndCategory aren't names you would normally use (I hope) and therefore you shouldn't.

Instead choose a shorter, better name and add a @Query annotation or a named query.

As for a the potential bug: If you can provide a reproducer, please report it at https://github.com/spring-projects/spring-data-jdbc/issues

Upvotes: 2

Related Questions