休眠中的通配符 [英] Wild card characters in hibernate

查看:82
本文介绍了休眠中的通配符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在休眠状态下添加通配符?

how to add a wildcard characters in hibernate?

我已经尝试这样做,但是已经出现错误

I have try to do like this but already have a errors

来自雇员,其中epf类似于" + Epf +%";

"from Employee where epf like"+Epf+"%";

epf是将int参数传递给查询

Epf is a int parameter pass to the query

推荐答案

您缺少引号'...'.最终查询应类似于:

You missing quotes '...'. The end-query should look like:

SELECT Employee WHERE epf LIKE 'text%'

因此将您的代码更改为

"from Employee where epf like '"+Epf+"%'";

应该可以解决问题. 注意:在此处'"+Epf+"%'

should do the trick. Note: open and close single quotes '...' here '"+Epf+"%'

但是你的方法不好.在查询中添加这样的文本很危险.考虑一下以获取更多信息:

But you approach is not good. Adding text like this to a query is dangerous. Consider this for more information:

使用绑定参数更安全:

session.createQuery("from Employee where epf like :epf")
       .setParameter("epf", epf + "%")
       .list();

这篇关于休眠中的通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆