我有一个DynamoDB表,具有以下键[customerid(HASH),sku(RANGE)]。
我正在尝试仅按客户ID查询。我目前收到此错误:
Caused by: java.lang.IllegalStateException: You have defined query method in the repository but you don't have any query lookup strategy defined. The infrastructure apparently does not support query methods!
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:545) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:324) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:297) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.util.Lazy.getNullable(Lazy.java:211) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.util.Lazy.get(Lazy.java:94) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:300) ~[spring-data-commons-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE]
... 30 common frames omitted
我已将queryLookupStrategy添加到我的应用程序中,希望它能起作用。尝试了几个值,但消息仍然存在。
@SpringBootApplication
@EnableDynamoDBRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
public class ProductItemApplication {
public static void main(String[] args) {
SpringApplication.run(ProductItemApplication.class, args);
}
}
有可能吗?还是Dynamo的局限性?由于Cassandra可以轻松做到这一点,因此我认为Dynamo也可以做到这一点,但可能是错误的。
我是否需要仅使用customerid定义索引来查询?如果是这样,用spring-data代码执行此操作的最佳方法是什么?
这是否是dynamo对spring数据的限制,要实现这一点,我需要显式使用dynamo客户端,而不是spring数据?
我的存储库是这样定义的:
interface CustomerItemRepository extends CrudRepository<CustomerItem, CustomerItemId> {
List<CustomerItem> findByCustomerId(String customerId);
}
我的钥匙是:
@NoArgsConstructor
@AllArgsConstructor
public class CustomerItemId {
private String customerId;
private String sku;
@DynamoDBHashKey(attributeName = "customerId")
public String getCustomerId() {
return customerId;
}
@SuppressWarnings("unused")
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
@DynamoDBRangeKey(attributeName = "sku")
public String getSku() {
return sku;
}
@SuppressWarnings("unused")
public void setSku(String sku) {
this.sku = sku;
}
}
最后,我的物品是:
@DynamoDBTable(tableName = "CustomerItem")
@NoArgsConstructor
public class CustomerItem {
@Id
private CustomerItemId id;
//... a few other non key fields...
CustomerItem(String customerId, String sku) {
this.id = new CustomerItemId(customerId, sku);
}
@DynamoDBHashKey(attributeName = "customerId")
public String getCustomerId() {
return id.getCustomerId();
}
@DynamoDBRangeKey(attributeName = "sku")
public String getSku() {
return id.getSku();
}
public void setCustomerId(String customerId) {
this.id = new CustomerItemId(customerId, id != null ? id.getSku() : null);
}
public void setSku(String sku) {
this.id = new CustomerItemId(id != null ? id.getCustomerId() : null, sku);
}
}
解决这个问题的最优雅的方法是什么? Dynamo似乎是一种便宜地存储这种简单数据的正确技术,但是现在我想我最好还是使用其他东西。
答案 0 :(得分:1)
我知道这不能解决您的问题,但是对于Java项目,我将DynamoDBMapper用于对象持久性,而不是Spring。它提供了一种将您的纯对象映射到DynamoDB的真正干净的方法,并且由AWS提供并支持。我已经使用了很多,并且DynamoDBMapper非常好。