我的服务中有以下方法:
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 在这个地方 -> for (Long item: holdedCardIds) {
我的 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
。但是,如果我尝试使用它,它将如何影响我的应用程序的性能?
那么,告诉我如何解决这个问题?
最佳答案
(long) can be casted into (int), and (int) can be casted to (long)
但是,
(Long) **cannot** be casted into (Integer)
反之亦然,因为它们不是原语。 bigint
这是您的根本问题,但我不确定它在您的程序中的哪个位置导致转换。
关于java - 尝试遍历实体 ID 时出现 ClassCastException : Integer cannot be cast to Long,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37105225/