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<PropertyStock> 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<PropertyStock> list = getHibernateTemplate().find(hql, customerid);
        System.out.println("查询的结果长度为:"+list.size());
        
//        Query query = session.createQuery(hql);
//        query.setLong(0, customerid);
//        List<PropertyStock> 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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值