[英]Search by the combination of multiple parameters via Native Query
I'm currently working on a fetaure that will allow the system to search public services receipts by the combination of 6 parameters which can be null meaning that receipts shouldn't be filtered by this parameter: accountNumber, amountRangeMin, amountRangeMax, dateRangeMin, dateRangeMax, publicServiceId. 我目前正在研究一种功能,它将允许系统通过6个参数的组合来搜索公共服务收据,这些参数可以为null,这意味着不应使用以下参数过滤收据:accountNumber,amountRangeMin,amountRangeMax,dateRangeMin,dateRangeMax, publicServiceId。 However making a method for each combination of the parameters is not an option, I'm thinking that there must be a better way, at first my approach was as following:
但是,为每个参数组合制作方法都不是一种选择,我认为必须有更好的方法,一开始我的方法如下:
On my Service I have this method: 在我的服务上,我有以下方法:
public Map<String,Object> findPublicServiceReceiptsByParams(Integer accountNumber, BigDecimal amountRangeMin,
BigDecimal amountRangeMax, LocalDate dateRangeMin, LocalDate dateRangeMax, Integer publicServiceId) {
Map<String,Object> publicServiceReceipts = new HashMap<String,Object>();
String accountNumberFilter = !(accountNumber==null) ? accountNumber.toString() : "AccountNumberTableName";
String amountRangeMinFilter = !(amountRangeMin==null) ? amountRangeMin.toString() : "table.AmountColumnName";
String amountRangeMaxFilter = !(amountRangeMax==null) ? amountRangeMax.toString() : "table.AmountColumnName";
String dateRangeMinFilter = !(dateRangeMin==null) ? dateRangeMin.toString() : "Table.ReceiptCreationDateColumn";
String dateRangeMaxFilter = !(dateRangeMax==null) ? dateRangeMax.toString() : "Table.ReceiptCreationDateColumn";
String publicServiceIdFilter = !(publicServiceId==null) ? publicServiceId.toString() : "table.publicServiceIdColumn";
publicServiceReceipts = publicServiceReceiptRepository.findPublicServiceReceiptsByParams(accountNumberFilter,
amountRangeMinFilter, amountRangeMaxFilter, dateRangeMinFilter, dateRangeMaxFilter,
publicServiceIdFilter);
return publicServiceReceipts;
}
And then in my repository I had: 然后在我的存储库中,我有:
final static String FIND_PUBLIC_SERVICES_BY_ARGS = "Select (Insert whatever logic should go in here to select columns from receipts the where clause is the one that matters)"
+ " WHERE ACT.ACT_AccountNumber=:accountNumberFilter\n"
+ " AND PSE.PSE_Id=:publicServiceIdFilter\n"
+ " AND PSR.PSR_CreateDate BETWEEN :dateRangeMinFilter AND :dateRangeMaxFilter\n"
+ " AND PSR.PSR_Amount BETWEEN :amountRangeMinFilter AND :amountRangeMaxFilter\n"
+ " order by PSR.PSR_CreateDate desc";
@Query(nativeQuery = true, value = FIND_PUBLIC_SERVICES_BY_ARGS)
Map<String, Object> findPublicServiceReceiptsByParams(@Param("accountNumberFilter") String accountNumberFilter,
@Param("amountRangeMinFilter") String amountRangeMinFilter,
@Param("amountRangeMaxFilter") String amountRangeMaxFilter,
@Param("dateRangeMinFilter") String dateRangeMinFilter,
@Param("dateRangeMaxFilter") String dateRangeMaxFilter,
@Param("publicServiceIdFilter") String publicServiceIdFilter);
} }
My reasoning was that if a parameter was null meant that whoever consumed the Web Service is not interested in that paramater so if that happens I set that variable as the Column Name so that it wouldn't affect in the WHERE clause and in theory make it simpler, but what I found was that It would send the names as Strings so it wouldn't be recognized as an sql statement which was the flaw in my thinking and as I said there must be another way other than writing each method for each combination, I appreciate any help :). 我的理由是,如果参数为null,则意味着使用Web Service的人对该参数都不感兴趣,因此,如果发生这种情况,我将该变量设置为Column Name,以使其在WHERE子句中不受影响,并且从理论上讲使它生效比较简单,但是我发现它会以字符串形式发送名称,因此不会被视为sql语句,这是我的思想上的缺陷,正如我所说,除了为每种组合编写每种方法外,还必须有其他方法,感谢您的帮助:)。
You should use the Criteria API , which was designed for creating dynamic queries. 您应该使用Criteria API ,该API是为创建动态查询而设计的。 Named queries aren't really meant to be used in this case.
在这种情况下,并不一定要使用命名查询。 With it you can do something like this:
有了它,您可以执行以下操作:
@PersistenceContext
EntityManager em;
List<YourEntity> method(String argument) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<YourEntity> cq = cb.createQuery(YourEntity.class);
Root<YourEntity> root = cq.from(YourEntity.class);
List<Predicate> predicates = new ArrayList<>();
if (argument == null) {
predicates.add(cb.equal(root.get("yourAttribute"), argument);
}
// rest of your logic goes here
cq.where(predicates.toArray(new Predicate[]{}));
return em.createQuery(cq).getResultList();
}
I found a way to fix this, I did it like this (I'm going to show only the native Query since it's the inly thing that i changed): 我找到了一种解决此问题的方法,我这样做是这样的(我将仅显示本机Query,因为这是我所更改的内容):
DECLARE @actNum varchar(50),@crdNum varchar(50),@pseId varchar(50),@dateMin varchar(50),@dateMax varchar(50),@amountMin varchar(50),@amountMax varchar(50)
SET @actNum = :actNum
SET @crdNum = :crdNum
SET @pseId = :pseId
SET @dateMin = :dateMin
SET @dateMax = :dateMax
SET @amountMin = :amountMin
SET @amountMax = :amountMax
--Whatever Select with joins statement
WHERE ACT.ACT_AccountNumber = CASE WHEN @actNum = 'N/A'
THEN ACT.ACT_AccountNumber
ELSE @actNum END
AND CRD_CardNumber = CASE WHEN @crdNum = 'N/A'
THEN CRD_CardNumber
ELSE @crdNum END
AND PSE.PSE_Id= CASE WHEN @pseId = 'N/A'
THEN PSE.PSE_Id
ELSE @pseId END
AND PSR.PSR_CreateDate >= CASE WHEN @dateMin = 'N/A'
THEN PSR.PSR_CreateDate
ELSE @dateMin END
AND PSR.PSR_CreateDate <= CASE WHEN @dateMax = 'N/A'
THEN PSR.PSR_CreateDate
ELSE @dateMax END
AND PSR.PSR_Amount BETWEEN CASE WHEN @amountMin = 'N/A'
THEN PSR.PSR_Amount
ELSE @amountMin END
AND CASE WHEN @amountMax = 'N/A'
THEN PSR.PSR_Amount
ELSE @amountMax END
ORDER BY PSR.PSR_CreateDate DESC
The backend will send the parameters as either "N/A" (if it shouldn't be used to filter data) or the actual value, this worked fine for me! 后端会将参数发送为“ N / A”(如果不应该用于过滤数据)或实际值,这对我来说很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.