how to add a wildcard characters in hibernate?
I have try to do like this but already have a errors
"from Employee where epf like"+Epf+"%";
Epf is a int parameter pass to the query
how to add a wildcard characters in hibernate?
I have try to do like this but already have a errors
"from Employee where epf like"+Epf+"%";
Epf is a int parameter pass to the query
你缺少引号'...'
。最终查询应如下所示:
SELECT Employee WHERE epf LIKE 'text%'
所以将您的代码更改为
"from Employee where epf like '"+Epf+"%'";
应该做的伎俩。注意:打开和关闭单引号'
...'
这里'"+Epf+"%'
但是你的方法不好。在查询中添加这样的文本是危险的。考虑这个以获取更多信息:
使用绑定参数更安全:
session.createQuery("from Employee where epf like :epf")
.setParameter("epf", epf + "%")
.list();
对于不区分大小写的 %Like% 搜索
Criteria criteria = session.createCriteria(Any.class);
criteria.add(Restrictions.ilike(propertyName, value, MatchMode.END);
criteria.list();
您可以为此使用标准构建器的 Like 功能。
int EPF=7;
CriteriaBuilder cb=session.getCriteriaBuilder();
CriteriaQuery<Object> cquery=cb.createQuery(Object.class);
Root<Employee> root=cquery.from(Employee.class);
cquery.select(root);
Predicate predicate=cb.like(root.<String>get("epf"),"EPF"+"%");
cquery.where(predicate);
Query q=session.createQuery(cquery);
System.out.println(q.list());