title: Inheritence using java generics not working as expected
body: <p>I am trying to use inheritence and generics to create my application, but it doesn't seem to work the way I expect it to. I'll show you what I mean (TL;DR at the bottom):</p>
  
  <pre><code>public interface IModel extends Serializable {
      public int save();
      public void update();
      public void delete();
  }
  
  // &lt;T&gt; is a JPA annotated entity/class
  @SuppressWarnings("serial")
  public abstract class Model&lt;T&gt; implements IModel {
      private final Repository&lt;T&gt; _repository;
      protected T _entity;
  
      public Model(T entity, Repository&lt;T&gt; repository) {
          this._entity = entity;
          this._repository = repository;
      }
  
      public int save() {
          return _repository.save(_entity);
      }
      ...
  }
  </code></pre>
  
  <p>This is implemented in for example my AccountModel, which is a Model with generic Account (which is a JPA entity) and which implements IAccount.</p>
  
  <pre><code>public class AccountModel extends Model&lt;Account&gt; implements IAccount {
      private static final AccountRepository REPOSITORY = new AccountRepository();
  
      public AccountModel(Account entity) {
          super(entity, REPOSITORY);
      }
      // Method implementations...
  }
  </code></pre>
  
  <p>My generic <code>Repository</code> looks like this:</p>
  
  <pre><code>public abstract class Repository&lt;T&gt; implements Serializable {
  
      private static SessionFactory SESSION_FACTORY;
      private final Class&lt;T&gt; _repositoryClass;
      private static boolean _initiated = false;
  
      public Repository(Class&lt;T&gt; repositoryClass) {
  
          if (!Repository._initiated) 
              setup();
  
          this._repositoryClass = repositoryClass;
      }
  
      private void setup() {
          // logics
          Repository._initiated = true;
      }
  
      public final Model&lt;T&gt; getById(int id) {
  
          Session session = SESSION_FACTORY.openSession();
  
          try {
              session.beginTransaction();
              T t = session.get(_repositoryClass, id);
              return new Model&lt;T&gt;(t, this); // As suggested by @Vlad
          }
          finally {
              session.close();
          }
      }
  }
  </code></pre>
  
  <p>The account implementation of this abstract Repository is:</p>
  
  <pre><code>public class AccountRepository extends Repository&lt;Account&gt; {
  
      public AccountRepository() {
          super(Account.class);
      }
  
      public Model&lt;Account&gt; getByEmail(String emailAddress) {...}
  }
  </code></pre>
  
  <p>So far so good, this is all working as expected. But I cannot use a <code>Model&lt;T&gt;</code> as a <code>TModel</code>.</p>
  
  <h2>TL;DR</h2>
  
  <p>I would like use the following line of code:<br>
  <code>AccountModel account = new AccountRepository().getById(1);</code><br>
  Since <code>AccountModel</code> inherits <code>Model&lt;Account&gt;</code> and <code>new AccountRepository().getById()</code> always returns <code>Model&lt;Account&gt;</code> I expect this to work, but it doesn't.</p>
  
  <p>What am I missing?</p>