简体   繁体   English

如何使用表达式LIKE使用JPA将条件中的String类型解析为Long?

[英]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: 我是新来的人,在编写方法时遇到问题,我尝试使用JPA和Spring用条件调用表达式,我有下一个代码:

@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" . 其中“ order_id”映射为long类型,我想传递类似“%5”的参数。

Can you help me with it? 你能帮我吗?

Regards!! 问候!!

Why this %5 ??? 为什么这个%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????? 它可以编译吗???

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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