Hibernate异常:Unable to locate appropriate constructor on class

1.这是Hibernate实体类操作异常,出现这个异常,要检查以下几个情况。

2.首先要检查在实体类中是否有构造器,例如:

public class PropertyStock {
    private String stockName;
    private Double stockPriceNow;
    private long num;//持有股票总数
    private long numDunjie;//冻结数量
    private Double price;//成交价钱
    private String stockCode;//股票代码
    private long customerId;
    //默认构造方法
    public PropertyStock() {}
    //有参数的构造方法, 返回的结果中有几个变量,就写几个变量的构造方法。
   public PropertyStock(Double stockPriceNow,String stockName,long num,long numDunjie,Double price ) {
       this.stockName=stockName;
       this.stockPriceNow=stockPriceNow;
       this.num=num;
       this.numDunjie=numDunjie;
       this.price=price;
   }

    public String getStockName() {
        return stockName;
    }
    public void setStockName(String stockName) {
        this.stockName = stockName;
    }
    public Double getStockPriceNow() {
        return stockPriceNow;
    }
    public void setStockPriceNow(Double stockPriceNow) {
        this.stockPriceNow = stockPriceNow;
    }
    public long getNum() {
        return num;
    }
    public void setNum(long num) {
        this.num = num;
    }
    public long getNumDunjie() {
        return numDunjie;
    }
    public void setNumDunjie(long numDunjie) {
        this.numDunjie = numDunjie;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price = price;
    }
    public String getStockCode() {
        return stockCode;
    }

    public void setStockCode(String stockCode) {
        this.stockCode = stockCode;
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

}

3.如果有上面两个构造器,再检查变量是否匹配,不要写错了

4.然后要注意,变量类型是否匹配,比如,上面的age变量类型为:Integer,写成 int可能会有问题。所以要注意long和Long,boolean和Boolean等等,最好写成long型

5.查询时有几个参数,你的构造器要对应相应的参数

public List findByCustomerId(java.lang.Long customerid){
        
        String hql="select new com.stock.model.PropertyStock(TS.stockPriceNow,TS.stockName,TP.num,TP.numDunjie,TP.price) from TStock as TS,TProperty as TP where TS.stockCode=TP.stockCode and TP.customerId=?";
        List list = getHibernateTemplate().find(hql, customerid);
        System.out.println("查询的结果长度为:"+list.size());
        
//        Query query = session.createQuery(hql);
//        query.setLong(0, customerid);
//        List list = query.list();
//        System.out.println("长度为:"+list.size());
        System.out.println(list.get(0).getCustomerId());
        System.out.println(list.get(0).getNum());
        System.out.println(list.get(0).getNumDunjie());
        System.out.println(list.get(0).getPrice());
        System.out.println(list.get(0).getStockPriceNow());
        return list;
    }

上面的hql语句对应的sql语句为:select t_stock.stock_price_now,t_stock.stock_name,t_property.num,t_property.num_dunjie,t_property.price from t_stock,t_property where t_property.customer_id=1 and t_stock.stock_code=t_property.stock_code

查询两种表中的部分属性,其中条件是stockcode相等,且customerid=1;


你可能感兴趣的:(经验积累)