在hibernate中执行复杂查询

时间:2017-04-18 10:00:56

标签: mysql spring hibernate

SELECT * FROM reviews 
   WHERE id in (SELECT review_id FROM reviews_businesses_users_bridge WHERE user_id = 1)
Union 
SELECT * FROM reviews 
   WHERE id in (select review_id FROMreviews_drivers_users_bridge WHERE user_id = 1);

此查询与Workbench完美配合,但是当我在我的应用程序中使用此查询时,它给出错误..

@Query(value = "SELECT * FROM reviews WHERE id in (SELECT review_id FROM reviews_businesses_users_bridge WHERE user_id = ?) Union SELECT * FROM reviews WHERE id in (select review_id FROM reviews_drivers_users_bridge WHERE user_id = ?)",nativeQuery = true)
List<Review> getAllReceivedReviewsByUserId(Long userId);

并且日志中的错误是&#34;没有找到处理程序方法&#34;,这对我没有任何意义,因为如果我让查询像

@Query(value = "SELECT * FROM reviews WHERE id in (SELECT review_id FROM reviews_businesses_users_bridge WHERE user_id = ?)",nativeQuery = true)
List<Review> getAllReceivedReviewsByUserId(Long userId);

然后它完美地工作,当我添加更多复杂性然后它显示错误。

1 个答案:

答案 0 :(得分:2)

尝试使用命名参数。你使用两个问号,所以hibernate也希望填充第二个参数

@Query(value = "SELECT * FROM reviews WHERE id in (SELECT review_id FROM reviews_businesses_users_bridge WHERE user_id = :userId) Union SELECT * FROM reviews WHERE id in (select review_id FROM reviews_drivers_users_bridge WHERE user_id = :userId)",nativeQuery = true)
List<Review> getAllReceivedReviewsByUserId(@Param("userId") Long userId);