为什么多个 Where 子句在本机查询中不起作用
Why Multiple Where Clause not Woking in nativeQuery
@Query(
nativeQuery = true,
value =
"SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name\n"
+ "FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id \n"
+ "JOIN users u ON u.id=tu.trip_user_id \n"
+ "WHERE td.trip_date BETWEEN :fromDate AND :toDate \n"
+ "AND u.id =: userId")
List<Object> getMobileForUpComingTripDetails(
@Param("userId") Integer userId,
@Param("fromDate") Date fromDate,
@Param("toDate") Date toDate);
它会报错
Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List com.dao.interfaces.TripDetailsDao.
getMobileForUpComingTripDetails(java.lang.Integer,java.util.Date,java.util.Date) but parameter 'userId' not found in annotated query 'SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name
FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id
JOIN users u ON u.id=tu.trip_user_id
WHERE td.trip_date BETWEEN :fromDate AND :toDate
AND tu.trip_user_id = : userId'!
但是当我们使用单个 where 子句时它起作用了
@Query(
nativeQuery = true,
value =
"SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name\n"
+ "FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id \n"
+ "JOIN users u ON u.id=tu.trip_user_id \n"
+ "WHERE td.trip_date BETWEEN :fromDate AND :toDate")
List<Object> getMobileForUpComingTripDetails(
// @Param("userId") Integer userId,
@Param("fromDate") Date fromDate,
@Param("toDate") Date toDate);
我的回答是
[
{
"tripDate": "15/02/2022",
"tripCode": "2sdfsdfklsfj0001",
"userName": "jkl.com",
"userId": 204
},
{
"tripDate": "15/02/2022",
"tripCode": "220xsdjffksf0001",
"userName": "abc.com",
"userId": 210
},
]
但我的问题是基于 queryParm 作为 userId 我们得到的数据不是全部,所以我添加了多个 where claues 我们得到错误请帮助我。
因此当您查看错误消息时,它显然表示在查询中找不到参数 userId
。这是因为您在查询中输入错误:
+ "AND u.id =: userId")
应该是这样的:
+ "AND u.id = :userId")
阅读错误消息总能帮助您进行调试。
@Query(
nativeQuery = true,
value =
"SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name\n"
+ "FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id \n"
+ "JOIN users u ON u.id=tu.trip_user_id \n"
+ "WHERE td.trip_date BETWEEN :fromDate AND :toDate \n"
+ "AND u.id =: userId")
List<Object> getMobileForUpComingTripDetails(
@Param("userId") Integer userId,
@Param("fromDate") Date fromDate,
@Param("toDate") Date toDate);
它会报错
Caused by: java.lang.IllegalStateException: Using named parameters for method public abstract java.util.List com.dao.interfaces.TripDetailsDao.
getMobileForUpComingTripDetails(java.lang.Integer,java.util.Date,java.util.Date) but parameter 'userId' not found in annotated query 'SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name
FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id
JOIN users u ON u.id=tu.trip_user_id
WHERE td.trip_date BETWEEN :fromDate AND :toDate
AND tu.trip_user_id = : userId'!
但是当我们使用单个 where 子句时它起作用了
@Query(
nativeQuery = true,
value =
"SELECT td.trip_date, td.trip_code, td.trip_distance, td.trip_travel_time, tu.status, tu.pickup_drop_time, u.id, u.user_name\n"
+ "FROM trip_details td JOIN trip_users tu ON tu.trip_details_trip_id=td.trip_id \n"
+ "JOIN users u ON u.id=tu.trip_user_id \n"
+ "WHERE td.trip_date BETWEEN :fromDate AND :toDate")
List<Object> getMobileForUpComingTripDetails(
// @Param("userId") Integer userId,
@Param("fromDate") Date fromDate,
@Param("toDate") Date toDate);
我的回答是
[
{
"tripDate": "15/02/2022",
"tripCode": "2sdfsdfklsfj0001",
"userName": "jkl.com",
"userId": 204
},
{
"tripDate": "15/02/2022",
"tripCode": "220xsdjffksf0001",
"userName": "abc.com",
"userId": 210
},
]
但我的问题是基于 queryParm 作为 userId 我们得到的数据不是全部,所以我添加了多个 where claues 我们得到错误请帮助我。
因此当您查看错误消息时,它显然表示在查询中找不到参数 userId
。这是因为您在查询中输入错误:
+ "AND u.id =: userId")
应该是这样的:
+ "AND u.id = :userId")
阅读错误消息总能帮助您进行调试。