Spring Data JPA ClassCastException:整型不能转换为Long [英] Spring Data JPA ClassCastException: Integer cannot be cast to Long

查看:1383
本文介绍了Spring Data JPA ClassCastException:整型不能转换为Long的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring Data应用程序中,我遇到了类似的问题,这里描述

这是我的实体:

  @Table(name =users)
public class用户扩展BaseEntity实现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 )
私人长ID;
...
}

和Spring Data Repository方法:

  @Query(value =SELECT u.user_id FROM users u WHERE u.game_id =:gameId,nativeQuery = true)
List< Long> getGameIds(@Param(gameId)Long gameId);

即将返回Long类型列表,但在执行后返回整数和应用程序列表失败, / p>

java.lang.ClassCastException:java.lang.Integer不能转换为java.lang.Long



如何判断Spring Data或JPA return Long (不是 Integer )在结果列表中?



我不想在运行时将值(整数转换为长整数)。

另外,我的应用程序的主要标准是性能,因此将我的ID从 Long 切换为整数在我所有的实体?在JPA中为实体ID使用 Integer 而不是 Long 是否是一种很好的做法?



UPDATED

我使用PostgreSQL



的情况:

user_id INTEGER 我收到 - java.lang.ClassCastException: java.lang.Integer不能转换为java.lang.Long



user_id BIGINT 我收到 - java.lang.ClassCastException:java.math.BigInteger不能转换为java.lang.Long

Long class不与你的数据库类型一致时 - getLong不会在那里工作。因此,您应该执行以下操作之一:


  1. 将db和in应用程序中的类型更改为BigInteger(如果integer不足以满足您的需要) / li>
  2. 将db和in app中的类型更改为Integer(如果足够满足您的需求)
  3. 删除nqtiveQuery属性并使用清晰的JPQL。


In my Spring Data application I ran into the similar problem described here ClassCastException: Integer cannot be cast to Long, while trying to iterate over entity IDs

This my entity:

@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;
...
}

and Spring Data Repository method:

@Query(value = "SELECT u.user_id FROM users u WHERE u.game_id =:gameId", nativeQuery = true)
List<Long> getGameIds(@Param("gameId") Long gameId);

which is going to return List of Long type but after execution returns List of Integer and application fails with

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

How to tell Spring Data or JPA return List of Long(not Integer) in the result list ?

I don't want to cast values(Integer to Long) in run-time.

Also, the main criterion of my application is performance so is it a good idea to switch my IDs from Long to Integer in all my entities ? Is it a good practice to use Integer instead of Long in JPA for entity ID ?

UPDATED

I use PostgreSQL

In case of:

user_id INTEGER I'm receiving - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long

user_id BIGINT I'm receiving - java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long

解决方案

Problem is when you are using native query Long class doesn't corellate with your database type — getLong doesn't work there. So you should do one of the following

  1. Change type in db and in app to BigInteger (if integer is not enough for your needs)
  2. Change type in db and in app to Integer (if it's enough for your needs)
  3. Remove nqtiveQuery attribute and use clear JPQL.

这篇关于Spring Data JPA ClassCastException:整型不能转换为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆