title: Spring-Data Repository @Query by class with generic class
body: <p>assuming I have the following entities:</p>
  
  <pre><code>@Entity
  public class Word { ... }
  
  @Entity
  public class Noun extends Word { ... }
  
  @Entity
  public class Verb extends Word { ... }
  </code></pre>
  
  <p>(Plus the usual Disriminator- and Join-Strategy stuff, simply assume that the entities work fine, which they do.)</p>
  
  <p>I tried ...</p>
  
  <pre><code>public interface WordRepository extends CrudRepository&lt;Word, Long&gt;{
  
       @Query("SELECT x FROM Word x WHERE type(x) = ?1")
       &lt;T extends Word&gt; List&lt;T&gt; findByClass(Class&lt;T&gt; clz);
  
  }
  </code></pre>
  
  <p>...but this gives me an exception, caused by:</p>
  
  <pre><code>org.hibernate.QueryException: Not all named parameters have been set: [1] [SELECT x FROM Word x WHERE type(x) = ?1]
  </code></pre>
  
  <p>One solution is to <strong>replace</strong> <code>Class&lt;T&gt;</code> with <code>Class&lt;?&gt;</code>, then the code works, but obviously that's not type-safe anymore since then I can write...</p>
  
  <pre><code>List&lt;Verb&gt; verbs = repository.findByClass(Noun.class);
  </code></pre>
  
  <p>...which runs, but obviously throws a <code>ClassCastException</code> whenever I try to access the <code>verbs (</code>since all the objects in the list are actually <code>Nouns</code>, not <code>Verbs</code>)</p>
  
  <p>Is there any way to write this type-safe with spring-data, preferably without hardcoding all types into their own methods (<code>findNouns</code>, <code>findVerbs</code>, etc.) or defining <code>Repositories</code> for all types?</p>
  
  <p>Edit: The problem seems to be the <code>Parameter.isDynamicProjectionParameter(MethodParameter)</code> method, that seems to define a special behavior for <code>Class&lt;T&gt;</code> parameters, so they are only used for dynamic parameters but cannot be given into the query itself. Hm. Wish anyone had a way around that.</p>