我的服务中有以下方法:
<?php
include 'users_db.php';
$first1=$_POST['name1'];
$last1=$_POST['lastname1'];
$username1=$_POST['user1'];
$pass1=$_POST['password1'];
$email01=$_POST['email1'];
$userinfo = $conn->prepare("INSERT INTO registered_users (FirstName, LastName, Username, Password, Email) VALUES (?, ?, ?, ?, ?)");
$userinfo->bind_param("sssss",$first1,$last1,$username1,$pass1,$email01);
$userinfo->execute();
// you shoud close the prep statement object
$userinfo->close();
//this is the way to kill the conn
$conn->close();
?>
当我想循环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) {
}
}
列表时,我收到: java.lang.ClassCastException:java.lang.Integer无法在此处强制转换为java.lang.Long - &GT; holdedCardIds
我的getExcludedCardIds()看起来像是:
for (Long item: holdedCardIds) {
存储库:
@Override
public List<Long> getExcludedCardIds(Board board) {
return boardCardRepository.getExcludedCardIds(board.getId());
}
的entites:
@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);
}
在我的POSTGRES schema.sql中,BoardCard实体的定义如下:
@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;
}
我找到here,postgresql中等效的LONG类型为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)
);
。但是,如果我尝试使用它,它将如何影响我的应用程序的性能方面?
那么,请告诉我该如何解决这个问题?
答案 0 :(得分: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);
}
答案 1 :(得分:0)
(long) can be casted into (int), and (int) can be casted to (long)
然而,
(Long) **cannot** be casted into (Integer)
反之亦然,因为它们不是原始的。同样适用于bigint
这是你的根本问题,虽然我不确定你的程序在哪里造成演员。
答案 2 :(得分:0)
如果您使用的是java-8,只需将Integer
个对象列表转换为包含相同值的对象,然后在long
中进行自动编码,即可解决此问题通过java。
getExcludedCardIds(prevBoard).stream
.mapToLong(x -> x).collect(Collectors.toList);