Hibernate query special characters such as % _
In SQL, %, _ and so on are special characters. Sometimes we need to query these special characters and escape them. The following two sentences are escaped.
Select username from gg_user where username like '% Xiao/_ %' escape '/';
Select username from gg_user where username like '% Xiao/%' escape '/';
The first SQL statement means that the _ after/is no longer used as a special character, and the second SQL statement means that the % after/is no longer used as a special character.
I listed my own piece of code in hibernate:
Public List Search (string username, string key, int page, int max ){
// Todo auto-generated method stub
Session session = hibernatesessionfactory. getsession ();
String hql = "from content c Where C. username in (select T. attenuser from attention t where T. Username = ?) And
C. Text like? Escape '/'order by C. Time DESC ";
Query q = session. createquery (hql );
Q. setstring (0, username );
Key = key. replaceall ("%", "/% ");
Key = key. replaceall ("_","/_");
Q. setstring (1, "%" + key + "% ");
Q. setmaxresults (max );
Q. setfirstresult (page-1) * max );
List list = Q. List ();
Session. Close ();
Return list;
}