![](/img/trans.png)
[英]JpaRepository - Hibernate - java.math.BigInteger cannot be cast to java.lang.Boolean
[英]class java.math.BigInteger cannot be cast to class java.lang.Boolean
我正在嘗試檢查該建築物中的房間名稱是否存在。如果存在,則不保存,否則將數據保存到表中。所以我編寫了這個查詢。
@Query(value = "select exists(select name,building from meetingroom " +
"where building=?1 and name=?2)",nativeQuery = true)
Boolean existsByRoom(String building,String name);
現在顯示 BigInteger cannot be cast to class java.lang.Boolean 有沒有辦法在不影響或更改查詢的情況下解決這個問題。
最簡單的解決方案是使用 JPA 派生查詢。 假設您的實體是 MeetingRoom
Boolean existsMeetingRoomByBuildingAndName(String building, String name);
一種解決方案是使用java.util.Optional
,如下所示。 並且您需要使用 BigInteger 而不是 Boolean 因為當記錄存在時查詢將返回 BigInteger 值。
@Query(value = "select exists(select name,building from meetingroom " +
"where building=?1 and name=?2)",nativeQuery = true)
Optional<BigInteger> existsByRoom(String building,String name);
在服務層,您可以檢查該值是否存在。
Optional<BigInteger> result =
meetingRoomRepository.existsByRoom(meetingRoomRequest.getBuilding(),meetingRoomRequest.getName());
if(result.isPresent()){
return true;
}
return false;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.