@Query doesn't work in Spring Boot (JPA ) + Azure Cosmos db

◇◆丶佛笑我妖孽 提交于 2021-01-06 07:54:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!