简体   繁体   中英

How parse a type String to Long in Criteria with JPA using expression LIKE?

I'm new here, I have a problem writting a method where I tried to call a expression with criteria using JPA and Spring, I have the next code:

@Override
    public List<ContractOrder> getOrdersByIn(List<String> paramsIn ) {
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<ContractOrder> query = builder.createQuery(ContractOrder.class);

        Root<ContractOrder> root = query.from(ContractOrder.class);

        Expression<String> exp = root.get("**order_id**");
        Predicate predicateIn = exp.**in**(paramsIn);

        ParameterExpression<Long> pexp = builder.parameter(Long.class,"order_id");
        Predicate predicateLike = builder.like(exp, pexp);

        query.where(builder.or(predicateIn,predicateLike));

        TypedQuery<ContractOrder> queryT = entityManager.createQuery(query.select(root));

        queryT.setParameter(0, Long.valueOf("%5"));

        List<ContractOrder> lista  = queryT.getResultList();

        return lista;
    }

Where "order_id" is mapping as type long and I want to pass a parameter like this "%5" .

Can you help me with it?

Regards!!

Why this %5 ??? It's a variable ???????

queryT.setParameter(0, Long.valueOf(5));

or

queryT.setParameter(0, Long.valueOf("5"));

should be OK.

Why ** and not just * ?I don't understand...

   Predicate predicateIn = exp.**in**(paramsIn);

Is it compile?????

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM