Hibernate异常:Unable to locate appropriate constructor on class

本文介绍了一种在使用Hibernate框架时出现的构造方法异常问题及其解决方案。问题表现为无法找到合适的构造函数,尤其当查询中使用了特定的字段组合时。文章提供了一个具体的案例,展示了如何通过添加合适的构造函数来解决这一问题。

  异常信息:org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class

org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [com.vrv.cems.assets.domain.Device] [select new Device(d.id,d.diskSize,d.diskSerial,d.registerTime) from com.vrv.cems.assets.domain.Device as d where d.matherBoard=:matherBoard]
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:263)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

  原因:Device类里面没有对应的构造方法

  注意:构造方法的参数都得对应才行。此外参数不能是Timestamp类型,有Timestamp类型的参数,也会报此错。

  解决方案:加上对应的构造参数即可。

public Device(String id,Integer diskSize ,String diskSerial) {
        super();
        this.id = id;
        this.diskSize = diskSize;
        this.diskSerial = diskSerial;
    }
public List<Device> queryByMatherBoardId(String matherBoardId) {
        String hql = "select new Device(d.id,d.diskSize,d.diskSerial) from Device as d where d.matherBoard=:matherBoard";
        return this.getSession().createQuery(hql)
                .setParameter("matherBoard", matherBoardId).list();
    }

 

转载于:https://www.cnblogs.com/goloving/p/7645743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值