简体   繁体   English

像运算符一样的休眠查询语言不起作用

[英]Hibernate query language like operator not working

cmTDsquery = createQuery("from CmTxnDetailStage cstg where cstg.statusCd=:err" +
                    " and (cstg.id.batchName like :batchname or cstg.id.batchName like :batchname1)"+
                    " and cstg.prodTypeBlfd like :search1 "+
                    " and trunc(cstg.tranUploadDttm)=:uploadDt");
cmTDsquery.bindStringProperty("search1", CmTxnDetailStage.properties.prodTypeBlfd, search);

I declared the search parameter as 我声明搜索参数为

search = "MON%";

If I replace the :search1 with 'MON%' ,It works fine and query returns result. 如果我将:search1替换为'MON%' ,它将正常工作并返回查询结果。

Only above code does not work and returns NULL . 仅上述代码不起作用,并返回NULL

Thanks 谢谢

----------------Edit 1 ----------------编辑1

cmTDsquery.bindStringProperty("batchname",CmTxnDetailStage.properties.batchName,batchNamePattern);
            cmTDsquery.bindStringProperty("batchname1",CmTxnDetailStage.properties.batchName,batchNamePattern1);

Sorry to mention this late , but above bindStringProperty methods works like a charm. 很抱歉在此提过很久,但是在bindStringProperty方法之上,它的作用就像是一种魅力。 batchname and batchname1 contains "CR_STLM_UBROL%" and "%RMB_PINDBT%". batchname和batchname1包含“ CR_STLM_UBROL%”和“%RMB_PINDBT%”。

Do like this while setting parameter 设置参数时这样做

"%"+search+"%" “%” +搜索+“%”

I can't understand exactly the line 我不明白这条线

cmTDsquery.bindStringProperty("search1",    CmTxnDetailStage.properties.prodTypeBlfd, search);

Do you set the correct value - "MON%" and not for example "MON"? 您是否设置了正确的值-“ MON%”,而不是例如“ MON”? I have used HQL like query.setParameter("search1","MON%") and it worked fine with like operators. 我已经使用过HQL,例如query.setParameter(“ search1”,“ MON%”),并且可以与类似的运算符一起正常工作。

My code is almost the same : 我的代码几乎相同:

whereClause = whereClause + (whereClause.length() > 0 ? "AND " : "") + "ac.name LIKE :name ";

And further in my code : 进一步在我的代码中:

if (searchCriteria.getNameSearchOperator() == SQL_STRING_SEARCH_OPERATORS.CONTAINS) {
    value = "%" + value + "%";
}
if (searchCriteria.getNameSearchOperator() == SQL_STRING_SEARCH_OPERATORS.START_WITH) {
    value = value + "%";
}
query.setParameter("name", value);

Try using setParameter instead of bindStringProperty . 尝试使用setParameter代替bindStringProperty

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

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