ClassCastException:整数无法转换为 Long,同时尝试迭代实体 ID

Posted

技术标签:

【中文标题】ClassCastException:整数无法转换为 Long,同时尝试迭代实体 ID【英文标题】:ClassCastException: Integer cannot be cast to Long, while trying to iterate over entity IDs 【发布时间】:2016-09-03 11:10:55 【问题描述】:

我的服务中有以下方法:

public Set<BoardCard> moveHoldedCardsToNewBoard(Board newBoard, Board prevBoard) 
        Set<BoardCard> boardCards = new HashSet<>();
        if (prevBoard != null) 
            List<Long> holdedCardIds = getExcludedCardIds(prevBoard);
            for (Long item: holdedCardIds) 

            
    

当我想循环holdedCardIds 列表时,我收到:java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in this place -> @ 987654324@

我的 getExcludedCardIds() 看起来像:

@Override
public List<Long> getExcludedCardIds(Board board) 

        return boardCardRepository.getExcludedCardIds(board.getId());
    

存储库:

@Repository
public interface BoardCardRepository extends JpaRepository<BoardCard, Long>, QueryDslPredicateExecutor<BoardCard> 

    @Query(value = "SELECT bc.card_id FROM boards_cards bc WHERE bc.board_id =:boardId AND bc.on_hold=true", nativeQuery = true)
    List<Long> getExcludedCardIds(@Param("boardId") Long boardId);

实体:

@Entity
@NamedEntityGraph(name = "graph.BoardCard", attributeNodes = )
@Table(name = "boards_cards")
public class BoardCard implements Serializable 

    private static final long serialVersionUID = -9019060375256960701L;

    @EmbeddedId
    private BoardCardId id = new BoardCardId();


@Embeddable
public class BoardCardId implements Serializable 

    private static final long serialVersionUID = -3630484760647866357L;

    @ManyToOne
    private Board board;

    @ManyToOne
    private Card card;


    @Entity
    @Table(name = "boards")
    public class Board extends BaseEntity implements Serializable 
        @Id
        @SequenceGenerator(name = "boards_id_seq", sequenceName = "boards_id_seq", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.AUTO, generator = "boards_id_seq")
        private Long id;





 @Entity    
   @Table(name = "cards")     
   public class Card extends BaseEntity implements Serializable 

                @Id
                @SequenceGenerator(name = "cards_id_seq", sequenceName = "cards_id_seq", allocationSize = 1)
                @GeneratedValue(strategy = GenerationType.AUTO, generator = "cards_id_seq")
                private Long id;

在我的 POSTGRES schema.sql 中,BoardCard 实体定义如下:

CREATE TABLE IF NOT EXISTS boards_cards(
                board_id INTEGER,
                card_id INTEGER,
                on_hold BOOLEAN DEFAULT FALSE,
                CONSTRAINT pk_user_card PRIMARY KEY (board_id, card_id),
                FOREIGN KEY(board_id) REFERENCES boards(id),
                FOREIGN KEY(card_id) REFERENCES cards(id)
            );

我发现 here ,在 postgresql 中相当于 LONG 类型的是 bigint 。但是,如果我尝试使用它,它将如何影响我的应用程序的性能方面?

那么,告诉我如何解决这个问题?

【问题讨论】:

【参考方案1】:

我找到了解决方案here。解决方案是使用 JPQL 查询 而不是 SQL 查询。

重构的存储库:

@Repository
public interface BoardCardRepository extends JpaRepository<BoardCard, Long>, QueryDslPredicateExecutor<BoardCard> 

    @Query(value = "SELECT id.card.id FROM BoardCard WHERE id.board.id = :boardId AND onHold = true")
    List<Long> getExcludedCardIds(@Param("boardId") Long boardId);

【讨论】:

【参考方案2】:
(long) can be casted into (int), and (int) can be casted to (long)

然而,

(Long) **cannot** be casted into (Integer)

反之亦然,因为它们不是原语。 bigint 也是如此

这是您的根本问题,尽管我不确定它在您的程序中的哪个位置导致了演员表。

【讨论】:

【参考方案3】:

如果您使用java-8,只需将Integer 对象列表转换为包含相同值的对象列表即可解决此问题,在long 中,然后将由java 自动装箱。

getExcludedCardIds(prevBoard).stream
                             .mapToLong(x -> x).collect(Collectors.toList);

【讨论】:

以上是关于ClassCastException:整数无法转换为 Long,同时尝试迭代实体 ID的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.ClassCastException:DTOObject 无法转换为 DTOObject

java.lang.ClassCastException:java.lang.String 无法转换为

ModelMapper 中的 ClassCastException:无法强制转换 EnhancerByModelMapper

iText 抛出 ClassCastException:PdfNumber 无法转换为 PdfLiteral

获取 java.lang.ClassCastException:无法强制转换 android.widget.SimpleAdapter

ClassCastException:java.lang.Object[] 无法转换为 java.lang.String[] android