title: How to reuse named query in multiple query methods in Spring data JPA?
body: <p>I want to figure out how to use <code>@NamedQuery</code> in Spring Data Jpa. As i understand, in plain JPA, <code>@NamedQuery</code> can be used for code organization: query is defined once and can be used anywhere as constant.</p>
  
  <p>I created an example to demonstrate problem. The query i used is very simple and normally can be expressed by repository method name. But it is just for example.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @NamedQueries({
      @NamedQuery(name = EXAMPLE_NAMED_QUERY,
                  query = "SELECT person FROM Person person WHERE person.id = :personId")
  })
  public class Person {
  
      public static final String EXAMPLE_NAMED_QUERY = "Person.exampleNamedQuery";
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String name;
  
      private String propertyOne;
  
      private String propertyTwo;
  
      protected Person() {}
  
      public Person(String name, String propertyOne, String propertyTwo) {
          this.name = name;
          this.propertyOne = propertyOne;
          this.propertyTwo = propertyTwo;
      }
  
      public Long getId() {
          return id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getPropertyOne() {
          return propertyOne;
      }
  
      public void setPropertyOne(String propertyOne) {
          this.propertyOne = propertyOne;
      }
  
      public String getPropertyTwo() {
          return propertyTwo;
      }
  
      public void setPropertyTwo(String propertyTwo) {
          this.propertyTwo = propertyTwo;
      }
  }
  </code></pre>
  
  <p>Now i want to use <code>@NamedQuery</code> in my SpringDataJPA-Repository:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface PersonRepository extends JpaRepository&lt;Person, Long&gt; {
  
      List&lt;PersonProjectionOne&gt; exampleNamedQueryProjectedByOne(@Param("personId") Long personId);
  
      List&lt;PersonProjectionTwo&gt; exampleNamedQueryProjectedByTwo(@Param("personId") Long personId);
  
  }
  </code></pre>
  
  <p>However, i get this error:</p>
  
  <p><code>PropertyReferenceException: No property exampleNamedQueryWithOne found for type Person!</code></p>
  
  <p>The documentation says that repository method that uses <code>@NamedQuery</code> must have name exactly as EntityName.XXX part in named query definition. I want to use the same <code>@NamedQuery</code> twice for two methods that returns same data but differently projected. According to this answer: <a href="https://stackoverflow.com/a/25057982/5728559">https://stackoverflow.com/a/25057982/5728559</a></p>
  
  <blockquote>
    <p>However, if you want to re-use the query definition on multiple query methods, using a named query is still a reasonable option.</p>
  </blockquote>
  
  <p>I thought that this is somehow possible. How?</p>