Spring Data JPA ClassCastException:整型不能转换为Long [英] Spring Data JPA ClassCastException: Integer cannot be cast to Long
问题描述
在我的Spring Data应用程序中,我遇到了类似的问题,这里描述 这是我的实体: 和Spring Data Repository方法: 即将返回Long类型列表,但在执行后返回整数和应用程序列表失败, / p> 如何判断Spring Data或JPA return 我不想在运行时将值(整数转换为长整数)。 另外,我的应用程序的主要标准是性能,因此将我的ID从 UPDATED 我使用PostgreSQL 的情况: 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: and Spring Data Repository method: which is going to return List of Long type but after execution returns List of Integer and application fails with How to tell Spring Data or JPA return List of 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 UPDATED I use PostgreSQL In case of: Problem is when you are using native query
这篇关于Spring Data JPA ClassCastException:整型不能转换为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
@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;
...
}
@Query(value =SELECT u.user_id FROM users u WHERE u.game_id =:gameId,nativeQuery = true)
List< Long> getGameIds(@Param(gameId)Long gameId);
java.lang.ClassCastException:java.lang.Integer不能转换为java.lang.Long
Long (不是
Integer $ c $> )在结果列表中?
Long
切换为整数
在我所有的实体?在JPA中为实体ID使用 Integer
而不是 Long
是否是一种很好的做法?
user_id INTEGER
我收到 - java.lang.ClassCastException: java.lang.Integer不能转换为java.lang.Long
user_id BIGINT
我收到 - java.lang.ClassCastException:java.math.BigInteger不能转换为java.lang.Long
@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;
...
}
@Query(value = "SELECT u.user_id FROM users u WHERE u.game_id =:gameId", nativeQuery = true)
List<Long> getGameIds(@Param("gameId") Long gameId);
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
Long
(not Integer
) in the result list ?Long
to Integer
in all my entities ? Is it a good practice to use Integer
instead of Long
in JPA for entity ID ?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
Long
class doesn't corellate with your database type — getLong doesn't work there. So you should do one of the following