title: Java:How to override this generic method?
body: <pre><code>public &lt;S extends T&gt; List&lt;S&gt; save(Iterable&lt;S&gt; entities) {
          //...
  }
  </code></pre>
  
  <p>If I use following method to override</p>
  
  <pre><code>@Override
  public List&lt;MyType&gt; save(Iterable&lt;MyType&gt; structures) {
      List&lt;MyType&gt; result = new ArrayList&lt;&gt;();
      //...
      return result;
  }
  </code></pre>
  
  <p>I get following error:</p>
  
  <pre><code>method does not override or implement a method from a supertype
  
  name clash: save(Iterable&lt;MyType&gt;) in MyTypeRepositoryImpl and &lt;S&gt;save(Iterable&lt;S&gt;) in SimpleJpaRepository have the same erasure, yet neither overrides the other
    where S,T are type-variables:
      S extends T declared in method &lt;S&gt;save(Iterable&lt;S&gt;)
      T extends Object declared in class SimpleJpaRepository
  </code></pre>
  
  <p>How can I solve this? I don't need the method to be generic and in fact it should not be. What I mean is that</p>
  
  <pre><code>@Override
  public &lt;S extends MyType&gt; List&lt;S&gt; save(Iterable&lt;S&gt; structures) {
      List&lt;S&gt; result = new ArrayList&lt;&gt;();
      //...
      return result;
  }
  </code></pre>
  
  <p>Will not work as the method can create a new Object of MyType which is not "compatible" to List. </p>
  
  <p>How can I make this work?</p>
  
  <p>EDIT:</p>
  
  <p>For clarification. I'm trying to override the different save() methods of Spring data SimpleJpaRepository (which is extented by QuerydslJpaRepository)</p>
  
  <p>Class defintions:</p>
  
  <pre><code>public class MyTypeRepositoryImpl
      extends QueryDslJpaRepository&lt;MyType, Long&gt;
      implements MyTypeRepository
  
  @NoRepositoryBean
  public interface MyTypeRepository
      extends JpaRepository&lt;MyType, Long&gt;,
      QueryDslPredicateExecutor&lt;MyType&gt; 
  </code></pre>
  
  <p>And this (from Spring Data)</p>
  
  <pre><code>public class QueryDslJpaRepository&lt;T, ID extends Serializable&gt; 
  extends SimpleJpaRepository&lt;T, ID&gt; 
  implements QueryDslPredicateExecutor&lt;T&gt;
  </code></pre>
  
  <p>EDIT 2:</p>
  
  <p>The method calls save(MyType entity) for each element and that method contains following logic:</p>
  
  <ol>
  <li>entity has a field which is unique</li>
  <li>get that fields value and check if entity with that value already exists</li>
  <li>if yes, use that entity (call to entityManager.merge) -> does not work returns MyType not S</li>
  <li>if no create a new one -> here new object is created. Does not work with generic type</li>
  </ol>
  
  <p>For 4. I can just set id = null and use the passed in object. That does not work for 3.</p>
  
  <p>So I'm very puzzled why this method has this signature. It makes it unusable for me and i don't get why I would save a subclass of T using Ts DAO. the save methods are the only ones with . All others just use T. I could just cast to S to make it compile but that seems ugly too...as any other type than T would lead to an exception.</p>