hibernate实体类构造方法内含有Timestamp类型变量时"Unable to locate approprite constructor"错误解决方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/arenn/article/details/76603813

在使用hibernate时,有时想获取部分字段的结果集,可以用如下方法:

可以在hql中使用select new 包名.类名(属性1,属性2,....)  from 实体类,同时要在实体类中添加带参的构造方法,参数的个数和顺序与(属性1,属性2,....)要保持一致,如此我们得到的List中存放的依然是实体类的对象。

例:

select new User(u.name,u.createTime) from User u order by u.createTime;

public User(String name,Timestamp createTime){
this.name = name;
this.createTime = createTime;
}

然后便遇到了如下错误:


解决方法:(2种)

public User(String name,Date createTime){
this.name = name;
this.createTime = Timestamp.valueOf(DateFormat.dateToNor(createTime))
}

or

public User(String name,Object createTime){
this.name = name;
this.createTime = Timestamp.valueOf(createTime.toString());
}


其中DateFormat是我自定义的工具类,其内的dateToNor()方法如下:

public static String dateToNor(Date dates){
		String time= date.format(dates);
		return time;
	}

public static SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

如此包含部分参数的构造函数便可匹配了。

参考博文:hibernate hql查询指定字段并获得结果集  hibernate中实体类构造方法中含有Timestamp 类型变量的"no appropriate constructor.." 错误提示的解决办法

展开阅读全文

Unable to locate appropriate constructor on class

06-20

看了半天,实在找不出来哪里有问题了.rnrnprivate Double damagegoodsfeesincome;//手工分摊收入rn private Double damagegoodsfeesexpenditure;//手工分摊支出rn rn private int waybillcount;//运单票数rn private Double waybilltotalfee;//运单总收入rn private Double waybillrealfee;//运单实际总收入rn rn private int transportbill_id;//配载单 IDrn private String vehiclenumberplate;//配载车辆-车牌号rn private Date loadingtime;//装车时间rn private String transportbillno;//装车单号rn private String loadingtype;//装车类型rn private Double directfee;//运费rn private Double arrivaldirectfee;//到站配送费rn private Double arrivaltransferfee;//到站中转费rn private String drivername;//司机姓名rnrnrnpublic TransportWaybillBillView(int transportbill_id,Date loadingtime,String transportbillno,String loadingtype,String vehiclenumberplate,rn String drivername,int waybillcount, Double waybilltotalfee, Double waybillrealfee,Double directfee,rn Double arrivaldirectfee,Double arrivaltransferfee,Double damagegoodsfeesincome,Double damagegoodsfeesexpenditure) rn super();rn this.transportbill_id = transportbill_id;rn this.loadingtime = loadingtime;rn this.transportbillno = transportbillno;rn this.loadingtype = loadingtype;rn this.vehiclenumberplate = vehiclenumberplate;rn this.drivername = drivername;rn this.waybillcount = waybillcount;rn this.waybilltotalfee = waybilltotalfee;rn this.waybillrealfee = waybillrealfee;rn this.directfee = directfee;rn this.arrivaldirectfee = arrivaldirectfee;rn this.arrivaltransferfee = arrivaltransferfee;rn this.damagegoodsfeesincome = damagegoodsfeesincome;rn this.damagegoodsfeesexpenditure = damagegoodsfeesexpenditure;rn rnrnString sql2 = "select new com.infotoall.view.TransportWaybillBillView(" +rn "tb.id,tb.loadingtime,tb.transportbillno,tb.loadingtype,tb.vehiclenumberplate,tb.drivername," +rn "count(w.id) as waybillcount,sum(w.totalfee) as waybilltotalfee," +rn "sum(w.realfee) as waybillrealfee,tb.totalfreight as directfee," +rn "tb.totalfreight as arrivaldirectfee,sum(w.transferfee) as arrivaltransferfee," +rn "sum(w.damagegoodsfeesincome),sum(w.damagegoodsfeesexpenditure)" +rn ") " +rn "from TransportBill tb,Waybill w " +rn "where tb.branch.id="+branch_id+" and w.transportbillno=tb.transportbillno GROUP BY tb.transportbillno";rnrnrn求大神看看 论坛

没有更多推荐了,返回首页