问题
I'm developing some IoT application. Data is in Azure Cosmos DB. example)
{
"id": "3ebd07c0-0740-466f-acb4-1e04a58cdf1a",
"serviceId": 1,
"deviceId": 1,
"contents": "{\"temperature\":34.797642257199705,\"humidity\":79.18982439419167,\"illuminance\":100}",
"date": 1552376519931
}
So. I want to use custom query like this
public interface DeviceTelemetryRepository extends DocumentDbRepository<DeviceTelemetry, String> {
@Query("SELECT a.deviceId FROM device_telemetry a where a.deviceId=:deviceId and a.date >=:from and a.date <=:to")
List<DeviceTelemetry> findTelemetryByDeviceId(@Param("deviceId") int deviceId,
@Param("from") long from,
@Param("to") long to );
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DeviceTelemetry {
private String id;
private int serviceId;
private int deviceId;
private String contents;
private long date;
}
But it doesn't work. I already tested it's query and data on Azure portal. It was ok.
I think that "DeviceTelemetryRepository extends DocumentDbRepository" cannot recognize @Query.
Do you have any suggestion about Spring Boot JPA + Azure Cosmos DB ? Thank you!
回答1:
Microsoft's spring-data-cosmosdb library doesn't support the @Query annotation.
回答2:
@Query("SELECT a FROM device_telemetry a where a.deviceId=:deviceId and a.date >=:from and a.date <=:to")
List<DeviceTelemetry> findTelemetryByDeviceId(@Param("deviceId") int deviceId,
@Param("from") Date from,
@Param("to") Date to );
回答3:
I changed the name of method. It works !
List<DeviceTelemetry> findDeviceTelemetryByDeviceIdAndDateBetween(@Param("deviceId") int deviceId,
@Param("from") long from,
@Param("to") long to );
but It doesn't still recognize @Query Please, let me know how to work with Query annotation.
回答4:
If anybody is still facing this, @Query
support has been incorporated in spring-data-cosmosdb 3.x
https://mvnrepository.com/artifact/com.azure/azure-spring-data-cosmos
来源:https://stackoverflow.com/questions/55134836/query-doesnt-work-in-spring-boot-jpa-azure-cosmos-db