2015-10-14 59 views
0

是否有可能通过hibernate搜索查询来查找类字段varibles的子字符串。例如:Hibernate,查找对象字段变量的子字符串

public class User { 

private String username = "asoftdrink"; 

} 

并做一个hibernate查询来查找类用户的所有对象,它们在变量用户名中包含子字符串“soft”。

回答

1

采取SQL看看Restrictions.like()

criteria.add(Restrictions.like("username", "soft", MatchMode.ANYWHERE)); 

这相当于

where username like '%soft%' 

。但请记住,由于数据库无法使用索引来执行查询,因此效率不高。

+0

谢谢,我会试试看。有没有更有效的方法? – JonCode