ClassCastException:在尝试迭代实体 ID 时,无法将整数转换为 Long
ClassCastException: Integer cannot be cast to Long, while trying to iterate over entity IDs
我的服务中有以下方法:
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-8,只需将您的 Integer
对象列表转换为包含相同值的对象列表即可解决此问题,然后在 long
中被 java.
自动装箱
getExcludedCardIds(prevBoard).stream
.mapToLong(x -> x).collect(Collectors.toList);
我找到了解决方案 。解决方案是使用 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);
}
我的服务中有以下方法:
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-8,只需将您的 Integer
对象列表转换为包含相同值的对象列表即可解决此问题,然后在 long
中被 java.
getExcludedCardIds(prevBoard).stream
.mapToLong(x -> x).collect(Collectors.toList);
我找到了解决方案
重构存储库:
@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);
}