title: Java Hibernate Named query not known
body: <p>Hi I am trying to use anotation to do named query. The line that caused  Named query not known is:</p>
  
  <pre><code>HibernateUtil.openSession().getNamedQuery(
                      "getWorkById")
  </code></pre>
  
  <p>Here is all the code about my named query. Actually I also tried to do so in mapping files, but I also got error and don't know what error is because it just reported internal failure. Any help about how to fix the error? Also, what is a better practice to do named query? Thanks!</p>
  
  <p>WorkModel:</p>
  
  <pre><code>package model;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.Table;
  
  
  @NamedQueries({
      @NamedQuery(name = "getWorkById", query = "from myday.work w where w.id = :id")
  })
  @Entity
  @Table(name = "myday.work")
  public class WorkModel {
      private Long id;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id", unique = true, nullable = false)
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  }
  </code></pre>
  
  <p>WorkModel hbm xml</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" &gt;
  
  &lt;hibernate-mapping&gt;
      &lt;class name="model.WorkModel" table="myday.work"&gt;
          &lt;id name="id" type="java.lang.Long"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="increment" /&gt;
          &lt;/id&gt;
      &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>hibernate cfg xml</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://XXX:3306&lt;/property&gt;
          // username, pwd setting...
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQL5Dialect&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Mapping files --&gt;
          &lt;mapping class="model.WorkModel"/&gt;
          &lt;mapping resource="model/WorkModel.hbm.xml"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>