ClassCastException:尝试迭代实体ID时,无法将Integer强制转换为Long [英] ClassCastException: Integer cannot be cast to Long, while trying to iterate over entity IDs
问题描述
我在服务中使用以下方法:
I have following method in my service:
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无法转换为Java。 lang.Long 在此位置-> 表示(长项:holdedCardIds){
When I want to loop the holdedCardIds
list, I received: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in this place -> for (Long item: holdedCardIds) {
我的getExcludedCardIds ()看起来像:
My getExcludedCardIds() looks like:
@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实体,如下所示:
In my POSTGRES schema.sql the BoardCard entity is defined, as follows:
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)
);
我发现中,等同于Postgresql中的LONG类型为 bigint
。但是,如果我尝试使用它,它将如何影响我的应用程序的性能?
I have found here , that equivalent of LONG type in postgresql is bigint
. But, If I try to use it, how it will affect on the performance side of my app?
那么,告诉我如何解决这个问题?
So, tell how can I solve this issue?
推荐答案
我在此处找到了解决方案。解决方案是使用 JPQL查询而不是SQL查询。
I have found solution here. The solution is to use JPQL query instead of SQL query.
重构的存储库:
@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);
}
这篇关于ClassCastException:尝试迭代实体ID时,无法将Integer强制转换为Long的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!