Sarthak Jain
Sarthak Jain

Reputation: 41

JAVA JPA Checking a date lies between two dates

I am using JAVA JPA Criteria Builder to check a whether a date lies between two dates or not. I am passing the date into the function which is having a query on a table and that table have two dates columns. Now i want to check the date which i have passed lies between the two or not.

 private CorpClientsCommRateD getCorpClientsCommRateD(Date date) {
    CorpClientsCommRateD commissionRate=null;
    try {
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<CorpClientsCommRateD> query = builder.createQuery(CorpClientsCommRateD.class);
        Root<CorpClientsCommRateD> root = query.from(CorpClientsCommRateD.class);

        List<Predicate> conditions = new ArrayList();
        conditions.add(builder.between(date,root.get("effFromDate"),root.get("effToDate")));

        query.select(root)
                .where(conditions.toArray(new Predicate[]{}));

        TypedQuery<CorpClientsCommRateD> commissionRates = em.createQuery(query);
        commissionRate=commissionRates.getSingleResult();
    }
    catch(Exception exp)
    {
        return commissionRate;
    }
    return commissionRate;
}

I am getting a error that Error:(148, 35) java: no suitable method found for between(java.util.Date,javax.persistence.criteria.Path,javax.persistence.criteria.Path)

Kindly help

Upvotes: 0

Views: 1343

Answers (1)

user8558216
user8558216

Reputation:

Your first argument to between is being passed as a Java literal, so to use that in Criteria you need to convert it to either a query parameter

builder.parameter(Date.class, "mydate")

or convert it to a Criteria literal.

builder.literal(date)

But then you could have got that by simply looking at the javadocs for that method and working out how to get a Criteria "Expression"

Upvotes: 1

Related Questions