1

Ok, so I'm still somewhat of a newbie to Java and hibernate and I'm trying to search for a question/answer set in my database, and I can pull up the set just fine if I type in the exact question, but when I use the like operator nothing works, and I'm really not sure what to do. I'm only searching for the question, and it's part of the same object as the answer, so I just pull up the answer with it as well.

Here's my code in my 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 my code in my 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 anyone could help me out with this that would be great, thanks.

Cœur
  • 37,241
  • 25
  • 195
  • 267
dochsner
  • 249
  • 2
  • 8
  • 25

1 Answers1

0

I don't see a "like" in your example but I assume you simply need to change Restrictions.eq ro Restrictions.like.

So if using Hibernate 4.3 this would be this method:

https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/criterion/Restrictions.html#like(java.lang.String, java.lang.Object)

I think a bit worrying is the "uniqueResult" afterwards, if you search with a wildcard I would always assume there might be more then one result. And in case there is the uniqueResult method might throw an Exception.

Also it always helps to enable "show_sql" in the Hibernate config to see the actual sql generated by Hibernate during development.

seba.wagner
  • 3,800
  • 4
  • 28
  • 52
  • 1
    Thanks, that worked great. I just changed my code to Restrictions.like. Also you were right about the "uniqueResult" throwing an exception. I took out the "uniqueResult" code and now nothing I search for works. Do you have any suggestions? This is what my code looks like now `return (QuestionAnswerSet) session.createCriteria(QuestionAnswerSet.class).add(Restrictions.like("question", "%"+question+"%"));` – dochsner Oct 05 '15 at 05:44
  • Actually It's a problem with my jsp I think, I don't have a c:forEach loop – dochsner Oct 05 '15 at 06:17
  • Yes, you need to say "forList()" and then handle the return type as a list type. Eventually you can just check if the list has more then 0 items and then just return the last one (if you are sure it will be the item that you want). – seba.wagner Oct 06 '15 at 02:36
  • 1
    I figured it out, I just added .list() in the place of the unique result and handled the return type as a list type updated the controller. How would I return the list of results in my .jsp, right now I'm using the

    ${searchResult.answer}

    ${searchResult.question}

    ` but it only returns one, just like I would expect, I just don't know how to return all the closest related results.
    – dochsner Oct 07 '15 at 20:44
  • see for example: http://stackoverflow.com/questions/16397207/iterate-arraylist-in-jsp – seba.wagner Oct 07 '15 at 21:49
  • Actually it works now with a – dochsner Oct 08 '15 at 23:19