title: Execute named query using HibernateTemplate
body: <p>I am developing a small application using Struts, Spring and hibernate3. But now I find trouble in executing named queries using HibernateTemplate.</p>
  
  <p>Here is my entity class, Product.java</p>
  
  <pre><code>@Entity
  @Table(name = "product")
  @NamedQueries({
  @NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p"),
  @NamedQuery(name = "Product.findByProductid", query = "SELECT p FROM Product p WHERE p.productid = :productid"),
  @NamedQuery(name = "Product.findByProductname", query = "SELECT p FROM Product p WHERE p.productname = :productname"),
  @NamedQuery(name = "Product.findByProductdesc", query = "SELECT p FROM Product p WHERE p.productdesc = :productdesc"),
  @NamedQuery(name = "Product.findByUnitprice", query = "SELECT p FROM Product p WHERE p.unitprice = :unitprice"),
  @NamedQuery(name = "Product.findByStockquantity", query = "SELECT p FROM Product p WHERE p.stockquantity = :stockquantity")})
  public class Product implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @Basic(optional = false)
  @Column(name = "productid")
  private Integer productid;
  @Basic(optional = false)
  @Column(name = "productname")
  private String productname;
  @Column(name = "productdesc")
  private String productdesc;
  @Basic(optional = false)
  @Column(name = "unitprice")
  private double unitprice;
  @Basic(optional = false)
  @Column(name = "stockquantity")
  private int stockquantity;
  
  public Product() {
  }
  
  public Product(Integer productid) {
      this.productid = productid;
  }
  
  public Product(Integer productid, String productname, double unitprice, int stockquantity) {
      this.productid = productid;
      this.productname = productname;
      this.unitprice = unitprice;
      this.stockquantity = stockquantity;
  }
  
  public Integer getProductid() {
      return productid;
  }
  
  public void setProductid(Integer productid) {
      this.productid = productid;
  }
  
  public String getProductname() {
      return productname;
  }
  
  public void setProductname(String productname) {
      this.productname = productname;
  }
  
  public String getProductdesc() {
      return productdesc;
  }
  
  public void setProductdesc(String productdesc) {
      this.productdesc = productdesc;
  }
  
  public double getUnitprice() {
      return unitprice;
  }
  
  public void setUnitprice(double unitprice) {
      this.unitprice = unitprice;
  }
  
  public int getStockquantity() {
      return stockquantity;
  }
  
  public void setStockquantity(int stockquantity) {
      this.stockquantity = stockquantity;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (productid != null ? productid.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Product)) {
          return false;
      }
      Product other = (Product) object;
      if ((this.productid == null &amp;&amp; other.productid != null) || (this.productid != null &amp;&amp; !this.productid.equals(other.productid))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "com.pojo.Product[productid=" + productid + "]";
  }
  
  }
  </code></pre>
  
  <p>Here is the DAO, ProductDAO.java</p>
  
  <pre><code>public class ProductDAO extends HibernateDaoSupport{
  
  public List listProducts() {
      return getHibernateTemplate().findByNamedQuery("Product.findAll");
  }
  }
  </code></pre>
  
  <p>But when I run the application, I am getting the following exception.</p>
  
  <pre><code>org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: Product.findAll
  </code></pre>
  
  <p>There is already a method- findNamedQuery()- in HibernateTemplate class to run named queries. And the queries are entered as annotations.</p>
  
  <p>Do I need to put that in the mapping file as well?</p>
  
  <p>Any thoughts please?</p>