简体   繁体   English

两侧的休眠“赞”表达式条件

[英]Hibernate “Like” expressions on two sides Criteria

I'd like to use criteria and create something like this but with hibernate criterias: 我想使用条件并创建类似这样的内容,但要具有休眠条件:

WHERE concat(X, Y) like '%Some Value%'

I don't want to do it using query concatenation, but using Restrictions because I am making a dynamic library that gets the left and right side of the like operator? 我不想使用查询级联来执行此操作,而是使用限制,因为我正在创建一个动态库,该库可以获取like运算符的左侧和右侧? Am I clear? 我清楚吗? Please if not ask me to give more clarification. 如果不请我进一步说明。

Thank you! 谢谢!

Hope it may works. 希望它能起作用。

Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.ilike("param1 || param2", "%" + dniWithLetter + "%"));

If the above code not working, please go through the following link. 如果以上代码无效,请通过以下链接。

Can we concatenate two properties in Hibernate HQL query? 我们可以在Hibernate HQL查询中连接两个属性吗?

I think that the best solution for your problem is the following 我认为,针对您的问题的最佳解决方案如下

...
Object[] params = {objX, objY, someValue};
Type[] typeOfParams = {Hibernate.STRING, Hibernate.STRING, Hibernate.STRING};
Criteria crit = session.createCriteria(Table.class);
crit.add(Restrictions.sqlRestriction("concat(?, ?) like '%?%'", params, typeOfParams));
...

When you use Restrictions.sqlRestriction , you can build a complex query with multiple parameters, you just have to set the parameters and type of each parameter, this is done with params array and typeOfparams array. 使用Restrictions.sqlRestriction ,可以构建具有多个参数的复杂查询,只需设置参数和每个参数的类型即可,这是通过params数组和typeOfparams数组完成的。

I hope this information helps you. 希望这些信息对您有所帮助。

Good luck. 祝好运。

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

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