title: Spring CrudRepository: why to invent a new generic type S
body: <p>Could you please give me a hint <strong>why in CrudRepository interface in the "save" method there is a new generic type "S" is invented</strong>. (see <a href="http://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html" rel="nofollow">org.springframework.data.repository.CrudRepository</a>)</p>
  
  <p>Here is an extraction for CrudRepository class:</p>
  
  <pre><code>public interface CrudRepository&lt;T, ID extends Serializable&gt;
           extends Repository&lt;T, ID&gt; {
      /**
       * Saves a given entity. Use the returned instance for further
       * operations as the save operation might have changed the
       * entity instance completely.
       * 
       * @param entity
       * @return the saved entity
       */
       &lt;S extends T&gt; S save(S entity);
  
       ...
  
       /**
        * Deletes a given entity.
        * 
        * @param entity
        * @throws IllegalArgumentException in case the given entity is (@literal null}.
        */
        void delete(T entity);
  }
  </code></pre>
  
  <p>Please note, in the second method "delete" the same entity has type T.
  The method "save" says it returns an object of type "S". So this is impossible to implement the method "save" say like this:</p>
  
  <pre><code>@Override
  public &lt;S extends MyType&gt; S save(S entity) {
      save(entity);
      return findOne(entity.getMyTypeId()); // Type mismatch: cannot convert from MyType to S
  }
  </code></pre>
  
  <p>The other question is how to implement the method "save" better?</p>