title: How to query DynamoDB table by hash using Spring Data
body: <p>I have a DynamoDB table with the following key [customerid (HASH), sku (RANGE)].</p>
  
  <p>I'm trying to query by customerid only. I'm currently getting this error:</p>
  
  <pre><code>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.&lt;init&gt;(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
  </code></pre>
  
  <p>I've added the queryLookupStrategy to my application in the hopes it worked. Tried the several values, but the message remains.</p>
  
  <pre><code>@SpringBootApplication
  @EnableDynamoDBRepositories(queryLookupStrategy = QueryLookupStrategy.Key.CREATE_IF_NOT_FOUND)
  public class ProductItemApplication {
      public static void main(String[] args) {
          SpringApplication.run(ProductItemApplication.class, args);
      }
  }
  </code></pre>
  
  <p>Is that possible? Or is it a limitation of Dynamo? Since Cassandra can easily do this, I'm assuming Dynamo would be able to do it too, but may be wrong. </p>
  
  <p>Would I need to define an index with only customerid to query this? If so, what's the best way to do that in code with spring-data?</p>
  
  <p>Or is this a limitation of spring data with dynamo, and to achieve this I'd need to use the dynamo client explicitly rather than spring data?</p>
  
  <p>My repository is defined as such:</p>
  
  <pre><code>interface CustomerItemRepository extends CrudRepository&lt;CustomerItem, CustomerItemId&gt; {
      List&lt;CustomerItem&gt; findByCustomerId(String customerId);
  }
  </code></pre>
  
  <p>And my key is:</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>
  
  <p>Finally, my item is:</p>
  
  <pre><code>    @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);
          }
      }
  </code></pre>
  
  <p>What's the most elegant way to solve this? Dynamo seemed the right tech to store this kind of simple data cheaply, but now I'm thinking I may be better off using something else.</p>