mysql - Like operator not working, using spring mvc and hibernate -
ok, i'm still of newbie java , hibernate , i'm trying search question/answer set in database, , can pull set fine if type in exact question, when use operator nothing works, , i'm not sure do. i'm searching question, , it's part of same object answer, pull answer well.
here's code in questionanswerdao
public questionanswerset getquestionanswersetbyquestion(string question) { session session = (session) em.getdelegate(); return (questionanswerset) session.createcriteria(questionanswerset.class).add(restrictions.eq("question", "%"+question+"%")).uniqueresult(); }
also here's code in controller
@requestmapping(value="search", method=requestmethod.get) public string searchget (modelmap model, httpservletrequest request) { searchform searchform = new searchform(); model.put("searchform", searchform); return "app/search"; } @requestmapping(value="search", method=requestmethod.post) public string searchpost (@modelattribute("searchform") searchform searchform, modelmap model, httpservletrequest request) { questionanswerset questionanswersetbyquestion = questionanswerdao.getquestionanswersetbyquestion(searchform.getsearchstring()); model.put("searchresult", questionanswersetbyquestion); return "app/search"; }
if me out great, thanks.
i don't see "like" in example assume need change restrictions.eq ro restrictions.like.
so if using hibernate 4.3 method:
https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/criterion/restrictions.html#like(java.lang.string, java.lang.object)
i think bit worrying "uniqueresult" afterwards, if search wildcard assume there might more 1 result. , in case there uniqueresult method might throw exception.
also helps enable "show_sql" in hibernate config see actual sql generated hibernate during development.
Comments
Post a Comment