【问题标题】:Execute named query using HibernateTemplate使用 HibernateTemplate 执行命名查询
【发布时间】:2012-01-01 12:14:16
【问题描述】:

我正在使用 Struts、Spring 和 hibernate3 开发一个小型应用程序。但是现在我发现使用 HibernateTemplate 执行命名查询时遇到了麻烦。

这是我的实体类 Product.java

@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 && other.productid != null) || (this.productid != null && !this.productid.equals(other.productid))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.pojo.Product[productid=" + productid + "]";
}

}

这里是 DAO,ProductDAO.java

public class ProductDAO extends HibernateDaoSupport{

public List listProducts() {
    return getHibernateTemplate().findByNamedQuery("Product.findAll");
}
}

但是当我运行应用程序时,我得到了以下异常。

org.springframework.orm.hibernate3.HibernateSystemException: Named query not known: Product.findAll

在 HibernateTemplate 类中已经有一个方法 findNamedQuery()- 来运行命名查询。并且查询作为注释输入。

我还需要把它放在映射文件中吗?

有什么想法吗?

【问题讨论】:

    标签: hibernate spring-mvc hibernate-mapping


    【解决方案1】:

    【讨论】:

    • 感谢伊兰!你真的救了我:)
    猜你喜欢
    • 1970-01-01
    • 2019-05-03
    • 2016-04-19
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2013-09-27
    • 2020-05-13
    相关资源
    最近更新 更多