Dealing with numbers using Spring ExampleMatcher
Asked Answered
R

1

8

I am new to Java and Spring, and I am building a sytem using Spring JPA. I am now working on my service and controller classes, and I would like to create a dynamic query. I have created a form, in which the user can enter values in the fields, or leave them blank. I then use example matcher to create an example based on non null fields and query objects in the database that match non null fields of the object.

It is working fine with Strings, and it works ok with numbers, in case the number entered by the user is matching the number in the database. What I would like to ask the community is: how can we, using Spring ExampleMatcher, add logic so that the query relating to numbers is not Select * from Projects where project.return = 10 but for instance Select * from Projects where project.return >=10?

It is a pretty basic question, but I have looked everywhere on the web, and I could not find an answer. All sources that I found said that ExampleMatcher deals only with Strings, but I find that strange that such a powerful system does not have a logic to deal with higherthan / lowerthan number type of criteria.

My code for the example matcher:

    ExampleMatcher matcher = ExampleMatcher.matching()
            .withIgnoreNullValues()
            .withIgnoreCase()
            .withIgnorePaths("projectId", "businessPlans", "projectReturn", "projectAddress.addressId")

I would like to add something like:

.withMatcher("projectAmountRaised", IsMoreThan(Long.parseLong()));

What I would have loved to have, but it is deprecated:

public static List getStockDailyRecordCriteria(Date startDate,Date endDate,
        Long volume,Session session){

Criteria criteria = session.createCriteria(StockDailyRecord.class);
if(startDate!=null){
        criteria.add(Expression.ge("date",startDate));
}
if(endDate!=null){
        criteria.add(Expression.le("date",endDate));
}
if(volume!=null){
        criteria.add(Expression.ge("volume",volume));
}
criteria.addOrder(Order.asc("date"));

return criteria.list();
  }

I am thus looking for something similar... I could create a broad results list from just Strings criteria using ExampleMatcher, and then write my own logic to delete objects that do not fit number criteria, but I am sure there is a more elegant approach.

Thank you a lot for your help, and for your indulgence!

Ripe answered 25/9, 2017 at 17:9 Comment(2)
Did you get a solution to thisSty
No, I just implemented my own solution: I used examplematcher to search for the strings, set it to ignore all other fields (using .withIgnorePaths()); and then I used if functions to erase results from the list that did not match my other criteria...Ripe
F
0

This is how you can use QBE and pageable with additional filters:

ExampleMatcher matcher = UntypedExampleMatcher.matching()
    .withIgnoreCase()
    .withIgnorePaths("startDate"); 

MyDao probe = new MyDao()

final Example<MyDao> example = Example.of(probe, matcher);
Query q = new Query(new Criteria().alike(example)).with(pageable);
q.addCriteria(Criteria.where("startDate").gte(probe.getStartDate()));

List<MyDao> list = mongoTemplate.find(q, example.getProbeType(), "COLLECTION_NAME");
PageableExecutionUtils.getPage(list, pageable, () -> mongoTemplate.count(q, example.getProbeType(), "COLLECTION_NAME"));
Frenum answered 1/6, 2022 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.