如何为下面给出的查询编写Java CriteriaBuilder

时间:2019-06-18 22:27:36

标签: java

如何在Java CriteriaBuilder中编写查询?

SELECT c FROM UserTransaction c WHERE  c.status in ('M', 'P') AND
instr(c.idNumber,:idNum) > 0 order by c.userI

注意:idNum是一个变量

请参阅我开始写的内容。但是我无法编译它。

CriteriaBuilder cb = em.getCriteriaBuilder();
ParameterExpression<String> idnumParameter = cb.parameter(String.class, idNum);
ParameterExpression<Integer> numberParameter = cb.parameter(Integer.class)       

CriteriaQuery<UserTransaction > cq = cb.createQuery(UserTransaction .class);
Root<UsasTransaction> postRoot = cq.from(UserTransaction .class);

Predicate predicateInM    = cb.equal(postRoot.get("status"),"M");
Predicate predicateInP    = cb.equal(postRoot.get("status"),"P");
Predicate predicateStatus = cb.or(predicateInM, predicateInP);

Predicate predicateInstr  = (Predicate) cb.function("INSTR", Boolean.class, postRoot.get("idNumber"), idnumParameter );
Predicate predicateGt     = cb.gt(predicateInstr, 0);

2 个答案:

答案 0 :(得分:0)

嗨,欢迎来到StackOverflow:)

首先,在发布问题时,请确保您提供与问题有关的所有信息。诸如“我无法编译”之类的术语太含糊。如您所知,编程中的细节很重要,因此我们需要更详细的信息才能帮助您:是否存在编译错误?如果是这样,错误消息是什么,在哪一行?等,等等。

还是,看一下代码,有几件事:

  • UsasTransaction-我认为那只是一个错字
  • 谓词。 请注意,在不同的软件包中有多个不同的类称为“谓词”,并且您的IDE可能默认建议输入错误的类,因此您需要确保导入正确的类。
  • cb.function-返回表达式,而不是谓词

将它们放在一起,请尝试以下操作:

    import javax.persistence.criteria.Expression;
    import javax.persistence.criteria.Predicate;

    String idNumParamName = "idnum";
    String idNumParamValue = "ABC123";

    CriteriaBuilder cb = em.getCriteriaBuilder();
    ParameterExpression<String> idnumParameter = cb.parameter(String.class, idNumParamName);
    ParameterExpression<Integer> numberParameter = cb.parameter(Integer.class) ;      

    CriteriaQuery<UserTransaction > cq = cb.createQuery(UserTransaction .class);
    Root<UserTransaction> postRoot = cq.from(UserTransaction .class);

    Predicate predicateInM    = cb.equal(postRoot.get("status"),"M");
    Predicate predicateInP    = cb.equal(postRoot.get("status"),"P");
    Predicate predicateStatus = cb.or(predicateInM, predicateInP);

    Expression<Integer> expressionInstr  = cb.function("INSTR", Integer.class, postRoot.get("idNumber"), idnumParameter );
    Predicate predicateGt     = cb.gt(expressionInstr, 0);

    cq.select(postRoot).where(cb.and(predicateStatus, predicateGt));

    UserTransactions = em.createQuery(cq).setParameter(idNumParamName, idNumParamValue).getResultList();

答案 1 :(得分:0)

                        So I ran the query slightly modified like this:  List<String> statusList = new ArrayList<String>();

statusList.add(“ R”); statusList.add(“ T”);

CriteriaBuilder cb = em.getCriteriaBuilder(); ParameterExpression idnumParameter = cb.parameter(String.class,idNum);

CriteriaQuery cq = cb.createQuery(UserTransaction .class); 根postRoot = cq.from(UserTransaction .class);

表达式expression = postRoot.get(“ status”); 谓词predicateStatus = expression.in(statusList);

//编辑以返回Integer 表达式expressionInstr = cb.function(“ INSTR”,Integer.class,postRoot.get(“ idNumber”),idnumParameter); 谓词谓词Gt = cb.gt(expressionInstr,0);

cq.select(postRoot).where(cb.and(predicateStatus,predicateGt)); cq.orderBy(cb.asc(postRoot.get(“ usasOutRecId”)));

UserTransactions = em.createQuery(cq).getResultList();

但是我收到此错误:org.hibernate.QueryException:尚未设置所有命名的参数:[param2] [从UserTransaction中选择generatedAlias0作为generateAlias0,其中((:param0,:param1)中的generateAlias0.status)和(

按INSTR(generatedAlias0.idNumber,:param2)> 0)顺序