title: How to query on a subset of composite primary key with @EmbeddedId or @Id key object (without using IdClass)?
body: <p>How does Spring Data JPA support querying on a <strong>subset of a primary key</strong>? Or will I need to implement a <strong>custom repository method</strong> for this?</p>
  
  <p>With classes like so:</p>
  
  <pre><code>@lombok.Data
  @lombok.NoArgsConstructor
  @lombok.AllArgsConstructor
  @Entity
  @Table(name = "my_entity")
  class MyEntity {
      @EmbeddedId
      private Pk id;
  
      public Pk getId() {
          return id;
      }
  
      public void setId(Pk id) {
          this.id = id;
      }
  
  }
  
  @lombok.Data
  @lombok.NoArgsConstructor
  @lombok.AllArgsConstructor
  @Embeddable
   class Pk implements Serializable {
      private Integer type;
      private String userId;
  
  
  }
  
  interface MyEntityRepository extends org.springframework.data.jpa.repository.JpaRepository&lt;MyEntity, Pk&gt; {
  
      java.util.Set&lt;MyEntity&gt; findAllByUserId(String userId);
  
  }
  </code></pre>
  
  <p>On start up my Spring Boot application I am getting the error:</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myEntityRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property user found for type MyEntity!
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:615)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:73)
      at com.myproject.backend.common.ServiceContext.start(ServiceContext.java:293)
      at com.myproject.run(AbstractServiceContainer.java:55)
      at com.myproject..run(AbstractServiceContainer.java:1)
      at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:42)
      at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:76)
      at io.dropwizard.cli.Cli.run(Cli.java:70)
      at io.dropwizard.Application.run(Application.java:72)
      at com.myproject..main(CombinedServiceContainer.java:106)
      Caused by: org.springframework.data.mapping.PropertyReferenceException: No property user found for type MyEntity!
      at org.springframework.data.mapping.PropertyPath.&lt;init&gt;(PropertyPath.java:75)
      at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
      at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359)
      at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
      at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
      at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
      at org.springframework.data.repository.query.parser.Part.&lt;init&gt;(Part.java:76)
      at org.springframework.data.repository.query.parser.PartTree$OrPart.&lt;init&gt;(PartTree.java:213)
      at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:321)
      at org.springframework.data.repository.query.parser.PartTree$Predicate.&lt;init&gt;(PartTree.java:301)
      at org.springframework.data.repository.query.parser.PartTree.&lt;init&gt;(PartTree.java:85)
      at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.&lt;init&gt;(PartTreeJpaQuery.java:60)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168)
      at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.&lt;init&gt;(RepositoryFactorySupport.java:320)
      at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
      at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
      at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
      ... 18 more
  </code></pre>
  
  <h2>Similar Questions</h2>
  
  <h3>IdClass solution</h3>
  
  <p>This is similar to the question <a href="https://stackoverflow.com/questions/28020699/how-to-query-on-subset-of-composite-primary-key">here</a> and should have been answered there except the OP decided to go another route for the solution by using IdClass. Though that solution works this question is to show that there is a supported way without doing IdClass.</p>
  
  <h3>Query By Example Solution</h3>
  
  <p><a href="https://stackoverflow.com/a/43882807/2084253">This answer</a> shows how to do it with Query By Example, which is great for more than one part of a composite key.</p>
  
  <p>This question is different than the linked answers because the linked answers show how to solve the problem by writing a query method name. This question asks how to solve the problem by writing an SQL or HQL string in the query annotation.</p>