我有一个如下所示的mysql查询,我在where子句中使用条件参数。如何在JPA中重写相同内容?
SELECT * FROM notification n where if(n.feature_details_id = 16,category_id IN(SELECT r.id FROM procurement_category r其中r.spendpool_id IN(SELECT spendpool_id FROM procurement_category p其中p.id IN(1,4))) ,category_id IN(4))和(recipient = 76或n.recipient_type =' ALL'和n.id NOT IN(从notification_statistics ns中选择ns.notification_id,其中ns.user_id = 76和ns.status = ' DISMISSED')和n.sender!= 76);
我试过这个。它仅在将单个值传递给param categoryIds时有效。
@Query("select n from Notification n "
+ "where (n.category.id IN(case when n.featureDetails.id=2 then(SELECT r.id FROM ProcurementCategory r where r.spendpoolId.id IN "
+ "(SELECT p.spendpoolId.id FROM ProcurementCategory p where p.id IN (:categoryIds))) "
+ "else (:categoryIds) end )) "
+ "and n.recipient =:recipient "
+ "or n.recipientType = 'ALL' "
+ "and n.category.id IN (SELECT r.id FROM ProcurementCategory r where r.spendpoolId.id IN "
+ "(SELECT p.spendpoolId.id FROM ProcurementCategory p where p.id IN (:categoryIds))) "
+ "and n.id NOT IN (select ns.notification.id from NotificationStatistics ns where ns.user.id =:recipient and ns.status = 'DISMISSED') "
+ "and n.sender != :recipient " )
如果传递多个值,则会出现错误,如
05-11-2016 09:58:32.153 [http-nio-8080-exec-3] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - 操作数应包含1列 05-11-2016 09:58:32.154 [http-nio-8080-exec-3] DEBUG cbiaop.logging.LoggingAspect - 输入:com.beroe.insync2.web.rest.errors.ExceptionTranslator.dataIntegrityViolationException()with argument [s]