I am using Spring Data and JPA in my application and I am trying to implement QueryDSL for dynamic criteria API. As far as if I send specific values in criteria, it works fine using below predicate:
Predicate predicate = QProductInfo.productInfo.shopName.eq(shopName).and(QProductInfo.productInfo.productType.eq(productType));
But if I receive multiple filter parameters and want to use a Map to store key-value pair of (column_name - column_value) to derive query dynamically, I am not able to create query for the same. Means I know I can add as many condition using and or other operator in Predicate but exactly how many expression I need to use is decided only at run time so not able to figure out the way to form right expression.
Here is some code information
@Entity
Public class ProductInfo{
productId;
title;
vendor;
code;
.... and more
}
Now filter can vary from 1 to n fields with values like filter 1 = product_id=123,title=test filter 2 =title= xyz, code= abc, vendor=pqr
So I will use map to store key-value pair(title-xyz and so on) and would like to construct query dynamically.
I went through many tutorial but could not find appropriate solution so far for my conditions. I thought of using Switch also while iterating loop for map, but how to club all expressions/predicates, I am not getting any idea.
If I don't find solution, probably I will use JPA Criteria API where we can use List of Predicates easily. Let me know if any information required to help me here.
Thanks