Ram Viswanathan
Ram Viswanathan

Reputation: 201

spring jpa criteria - trunc and to_date on the where clause

I need to build the following query statement in my Spring JPA layer.

select *  from PAYMENTS sellerpaym0_ 
where trunc(sellerpaym0_.PAYMENT_DATE)=TO_DATE('2019-05-26','yyyy-MM-dd'));

In my Java code, I have

   public class MySpecification<E> implements Specification<E> {

    private SearchCriteria criteria;


    @Override
    public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

  if (criteria.getOp().equalsIgnoreCase("=")) {
       return builder.equal(root.get(criteria.getKey()), criteria.getValue());
}
}

public static class MySpecificationBuilder {
        private String key;
        private String op;
        private String value;


        public static MySpecification spec(String key, String op, Comparable value) {
            return new MySpecification(new SearchCriteria(key, op, value));
        }
    }

The toPredicate method works just fine when the operator is "=", and I need no custom manipulation.

However when I need to represent the SQL where trunc(sellerpaym0_.PAYMENT_DATE)=TO_DATE('2019-05-26','yyyy-MM-dd'));

I am not able to understand how this can be done.

Any help would be much appreciated.

Upvotes: 0

Views: 2526

Answers (1)

Dominik
Dominik

Reputation: 353

Use CriteriaBuilder.function method, docs

Upvotes: 2

Related Questions