title: Hibernate SQLQuery - Get Object by name
body: <p>I have a method to get a Category by its "id" and I need a similar method to get a Category by its "name". I did these methods by using Hibernate. How can I fix my second method to get a Category by name?
  My source code is as follow: </p>
  
  <pre><code>// It works
  @Override
  public Category getById(int id) {
      return (Category) sessionFactory.getCurrentSession().get(Category.class, id);
  }
  
  // It doesn't works
  @Override
  public Category getByName(String name) {
      return (Category) sessionFactory.getCurrentSession().
          createSQLQuery("SELECT FROM Category WHERE name="+name);
  }
  </code></pre>
  
  <p>I have this error with the second method:</p>
  
  <blockquote>
    <p>java.lang.ClassCastException: org.hibernate.impl.SQLQueryImpl cannot
    be cast to com.sedae.model.Category</p>
  </blockquote>
  
  <p>These are my controllers.</p>
  
  <pre><code>@RequestMapping(value = "/getCategoryById/{id}")
  public String getCategoryById(Model model, @PathVariable ("id") int id){
      model.addAttribute("category", categoryService.getById(id));
      return "/getCategoryById";
  }
  
  @RequestMapping(value = "/getCategoryByName/{name}")
  public String getCategoryByName(Model model, @PathVariable("name") String name){
      model.addAttribute("category", categoryService.getByName(name));
      return "/getCategoryByName";
  }
  </code></pre>
  
  <p>Thanks in advance people.</p>