title: Spring Data JPA ClassCastException: Integer cannot be cast to Long
body: <p>In my Spring Data application I ran into the similar problem described here <a href="https://stackoverflow.com/questions/37105225/classcastexception-integer-cannot-be-cast-to-long-while-trying-to-iterate-over">ClassCastException: Integer cannot be cast to Long, while trying to iterate over entity IDs</a></p>
  
  <p>This my entity:</p>
  
  <pre><code>@Table(name = "users")
  public class User extends BaseEntity implements Serializable {
  
      private static final long serialVersionUID = 5088960286161030648L;
  
      @Id
      @SequenceGenerator(name = "users_id_seq", sequenceName = "users_id_seq", allocationSize = 1)
      @GeneratedValue(strategy = GenerationType.AUTO, generator = "users_id_seq")
      private Long id;
  ...
  }
  </code></pre>
  
  <p>and Spring Data Repository method:</p>
  
  <pre><code>@Query(value = "SELECT u.user_id FROM users u WHERE u.game_id =:gameId", nativeQuery = true)
  List&lt;Long&gt; getGameIds(@Param("gameId") Long gameId);
  </code></pre>
  
  <p>which is going to return List of Long type but after execution returns List of Integer and application fails with </p>
  
  <p><code>java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long</code></p>
  
  <p>How to tell Spring Data or JPA return List of <code>Long</code>(not <code>Integer</code>) in the result list ?</p>
  
  <p>I don't want to cast values(Integer to Long) in run-time.</p>
  
  <p>Also, the main criterion of my application is performance so is it a good idea to switch my IDs from <code>Long</code> to <code>Integer</code> in all my entities ? Is it a good practice to use <code>Integer</code> instead of <code>Long</code> in JPA for entity ID ?</p>
  
  <p><strong>UPDATED</strong></p>
  
  <p>I use PostgreSQL</p>
  
  <p>In case of: </p>
  
  <p><code>user_id INTEGER</code> I'm receiving - <code>java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long</code></p>
  
  <p><code>user_id BIGINT</code> I'm receiving - <code>java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long</code></p>