我的要求是:
createrIds
为空@Query("from CommodityEntity e where (:createrIds is null or e.createrEntity.id in :createrIds)" )
Page<CommodityEntity> searchByCreaterAndPage( @Param("createrIds") List<Long> createrIds,Pageable pageable);
不为空,则匹配数据以下代码不起作用,我该如何解决?
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent modify_intent = new Intent(ReadData.this,Read2.class);
HashMap<String, String> selected_item = Item_List.get(position);
modify_intent.putExtra("item", selected_item.get(ITEM_ID));
startActivity(modify_intent);
}
答案 0 :(得分:0)
我在(coalesce(:createrIds, null) is null or e.createrEntity.id in (:createrIds))
上取得了成功,但是否可行可能取决于您使用的数据库。
答案 1 :(得分:0)
考虑将查询分为2个不同的较小查询,并在界面中使用default
关键字来实施检查:
default Page<CommodityEntity> searchByCreaterAndPage( @Param("createrIds") List<Long> createrIds,Pageable pageable) {
if (createrIds == null || createrIds.isEmpty()) {
return searchAllByPage(pageable);
} else {
return searchByNonNullCreaterAndPage(createrIds, pageable);
}
}
@Query("from CommodityEntity e where e.createrEntity.id in :createrIds" )
Page<CommodityEntity> searchByNonNullCreaterAndPage(@Param("createrIds") List<Long> createrIds, Pageable pageable);
@Query("from CommodityEntity e" )
Page<CommodityEntity> searchAllByPage(Pageable pageable);
此解决方案具有2个优点: