0

I am able to fetch records using

`Criteria crit = session.createCriteria(GenerateInvoiceBean.class);
crit.add(Restrictions.eq("dealerId", dealerId)).add(Restrictions.between("billDate", frDate, tDate));
List<GenerateInvoiceBean> gib = crit.list();
            return gib;`

As createCriteria is deprecated in Hibernate 5.1.16, I am trying to use

`CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<GenerateInvoiceBean> query = builder.createQuery(GenerateInvoiceBean.class);

            Root<GenerateInvoiceBean> root = query.from(GenerateInvoiceBean.class);
            query.select(root).where(builder.equal(root.get("dealerId"), dealerId)).where(builder.between(root.get("billDate"), frDate, tDate))
            .orderBy(builder.asc(root.get("billNo")));

            List<GenerateInvoiceBean> gib = session.createQuery(query).getResultList();
            return gib;`

which is not working. Any solution or suggestion would be great! Thanks

4
  • 1
    why are you calling where multiple times? It will use the last one only! Pass multiple clauses in to the sole call to where. Clearly "is not working" ought to have lead you to look at the LOG for the JPA provider and decide WHAT "is not working", and then tell people that Commented Apr 30, 2018 at 14:32
  • Yes you are right @DN1 Sir, only the last where part is working. Can you help me resolving it? Hibernate: select * from Invoice generatein0_ where generatein0_.bill_date between ? and ? order by generatein0_.bill_no asc Hibernate is resolved to this query on log. Commented Apr 30, 2018 at 14:42
  • 1
    I already did, above. where can take in MULTIPLE clauses. So pass in multiple clauses to a single call of where!! Commented Apr 30, 2018 at 14:44
  • That solved it! Thanks a lot Sir @DN1 ps:I overlooked that multiple clauses comment, sorry. Commented Apr 30, 2018 at 14:47

1 Answer 1

1

Change your CriteriaQuery call to be

query.select(root)
    .where(builder.equal(root.get("dealerId"), dealerId), 
           builder.between(root.get("billDate"), frDate, tDate))
    .orderBy(builder.asc(root.get("billNo")));

so both where clauses are processed together in one call.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.