title: On Spring, how to query DynamoDB table using JPA and a composite key (Partition Key and Sort Key)?
body: <p>I have a Spring project set up with JPA and Spring Data DynamoDB. It works alright. I can obtain items from the DynamoDB table by reading it by Partition Key and the Sort key (referred as <code>DynamoDBHashKey</code> and <code>DynamoDBRangeKey</code>).</p>
  
  <p>My problem is that the way my repository is set up, the table is being read using <code>query</code> and <code>scan</code> operations, instead of <code>get-item</code> operation, which should be more efficient.</p>
  
  <p>This is my entity:</p>
  
  <pre><code>import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
  import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
  import lombok.AllArgsConstructor;
  import lombok.Builder;
  import lombok.Getter;
  import lombok.NoArgsConstructor;
  import lombok.Setter;
  import org.springframework.data.annotation.Id;
  
  @Getter
  @Setter
  @Builder
  @NoArgsConstructor
  @AllArgsConstructor
  @DynamoDBTable(tableName = "my-entity-table")
  public class MyEntity {
  
      @Id
      @DynamoDBHashKey 
      @DynamoDBAttribute(attributeName = "partition_key")
      private String partitionKey;
  
      @Id
      @DynamoDBRangeKey
      @DynamoDBAttribute(attributeName = "sort_key")
      private String sortKey;
  
      ...
  }
  </code></pre>
  
  <p>And this is my repository:</p>
  
  <pre><code>import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
  import org.springframework.data.repository.CrudRepository;
  import org.springframework.stereotype.Repository;
  
  import java.util.List;
  
  @EnableScan
  @Repository
  public interface MyEntityRepository extends CrudRepository&lt;MyEntity, String&gt; {
      List&lt;MyEntity&gt; findByPartitionKeyAndSortKey(String partitionKey, String sortKey);
  }
  </code></pre>
  
  <p>How do I configure my entity and repository to read items from the table using the <code>get-item</code> operation when my table has both a Partition Key and Sort Key?</p>