Unable to locate appropriate constructor on class解决方案

参考:http://blog.sina.com.cn/s/blog_4ad7c2540102uzkc.html

最近使用hibernate做项目,在配置实体类的时候有些字段使用了Timestamp类型。一直没出现什么问题,直到今天,使用了hql语句:String hql = “select new BasicSetting(id,subject,content,modifyDt) from BasicSetting”; 但是这样就报错了,错误如下:

org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.sydecm.entity.BasicSetting]. Expected arguments are: int, java.lang.String, java.lang.String, java.util.Date [select new BasicSetting(id,subject,content,modifyDt) from com.sydecm.entity.BasicSetting]
··········省略其他··············

问题:我明明是Timestamp类型,为何报错为 java.util.Date呢?难道不能自动转?但是我如果把hql语句改为“from BasicSetting”是没错的,谁知道具体原因麻烦留言下

解决方案

实体类增加个静态方法和创建与hql语句相对应的有参构造方法

public static Timestamp stringToTimestamp(String dateStr){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
           try {
                Date date = sdf.parse(dateStr);
                cal.setTime(date);
                return new Timestamp(cal.getTimeInMillis());
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            cal.setTime(new Date());
            return new Timestamp(cal.getTimeInMillis());
        }
    }

public BasicSetting(Integer id, String subject, String content, Object modifyDt) {
        super();
        this.id = id;
        this.subject = subject;
        this.content = content;
        this.modifyDt = stringToTimestamp(modifyDt.toString());
    }

dao的实现方法

public List<BasicSetting> queryBasicSetting() throws Exception{
        String hql = "select new BasicSetting(id,subject,content,modifyDt) from BasicSetting";
        Query query = super.createQuery(hql);
        List<BasicSetting> basicSettings = query.list();
        return basicSettings;
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值