title: Olingo - Create strongly typed POJOs for client library of OData service
body: <p>I'm using Apache Olingo as an OData client for a Java SDK that I will provide for a RESTful OData API. In the SDK I want to be able to have strongly typed classes to represent the OData entities.  I'm having trouble implementing this easily and thus feel like I'm missing a different strategy here.</p>
  
  <p>The Olingo way seems to be to get an <code>ODataClient</code> object which provides the user with a bunch of useful methods for interacting with the API. The <code>ODataClient</code> is using a bunch of factory methods to build my request.  For instance, this is the code I used to get the <code>Customers</code> from the Northwind sample OData service. <code>client</code> is an an instance of the necessary <code>ODataClient</code> class.</p>
  
  <pre><code>String serviceRoot = "http://services.odata.org/V4/Northwind/Northwind.svc";
  URI customersUri = client.newURIBuilder(serviceRoot)
          .appendEntitySetSegment("Customers").build();
  ODataRetrieveResponse&lt;ODataEntitySetIterator&lt;ODataEntitySet, ODataEntity&gt;&gt; response =
          client.getRetrieveRequestFactory().getEntitySetIteratorRequest(customersUri).execute();
  
  if (response.getStatusCode() &gt;= 400) {
      log("Error");
      return;
  }
  
  ODataEntitySetIterator&lt;ODataEntitySet, ODataEntity&gt; iterator = response.getBody();
  
  while (iterator.hasNext()) {
      ODataEntity customer = iterator.next();
      log(customer.getId().toString());
  }
  </code></pre>
  
  <p>I'd like to end up with a strongly typed entity from the iterator (i.e. <code>Customer customer = iterator.next()</code>).  However, I'm not sure how to actually do that.</p>
  
  <p>If I create a <code>Customer</code> class that extends <code>ODataEntity</code> and attempt to perform a cast such as <code>Customer customer = (Customer) iterator.next()</code> then I get a <code>ClassCastException</code> since the objects in the iterator are just <code>ODataEntity</code> objects and know nothing about the <code>Customer</code> subclass.</p>
  
  <p>My next thought was to introduce generics but doing so will require what seems like a good amount of modification to the Olingo library which leads me to think that there is a better way to do this.</p>
  
  <p>I'm using the development version of Apache Olingo 4 since the OData service must use OData 4.</p>
  
  <p>What am I missing?</p>