Hibernate Transformers.aliasToBean填充主要字段

时间:2018-04-05 11:06:31

标签: sql-server hql

我正在尝试使用带有2个主键字段的Tbcompany来获取Transformers.aliasToBean表的列表。

我正在使用SQL SERVER和Hibernate 3.2.4。

我的表有2个主要字段。

Tbcompany.class

public class Tbcompany {

    private TbcompanyId id;
    private String hcompanycode;


    public TbcompanyId getId() {
        return id;
    }

    public void setId(TbcompanyId id) {
        this.id = id;
    }

    public String getHcompanycode() {
        return hcompanycode;
    }

    public void setHcompanycode(String hcompanycode) {
        this.hcompanycode = hcompanycode;
    }
}

在TbcompanyId.class里面:

public class TbcompanyId
    implements Serializable
{

private String companycode;
private String companyname;

public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (!(o instanceof TbcompanyId)) {
        return false;
    }
    TbcompanyId other = ((TbcompanyId) o);
    if (this.companycode == null) {
        if (other.companycode!= null) {
            return false;
        }
    } else {
        if (!this.companycode.equals(other.companycode)) {
            return false;
        }
    }
    if (this.companyname == null) {
        if (other.companyname!= null) {
            return false;
        }
    } else {
        if (!this.companyname.equals(other.companyname)) {
            return false;
        }
    }
    return true;
}

public int hashCode() {
    int rtn = 17;
    rtn = (rtn* 37);
    if (this.companycode!= null) {
        rtn = (rtn + this.companycode.hashCode());
    }
    rtn = (rtn* 37);
    if (this.companyname!= null) {
        rtn = (rtn + this.companyname.hashCode());
    }
    return rtn;
}

public String getCompanycode() {
    return companycode;
}

public void setCompanycode(String companycode) {
    this.companycode = companycode;
}

public String getCompanyname() {
    return companyname;
}

public void setCompanyname(String companyname) {
    this.companyname = companyname;
}

我想创建一个表单并使用Transformers.aliasToBean来填充表单。

此查询:

Query q;
q = session.createQuery("SELECT a.id.companycode as companycode,a.id.companyname as companyname,a.hcompanycode as hcompanycode FROM Tbcompany a");
q.setResultTransformer(Transformers.aliasToBean(Tbcompany.class));
list = q.list();

给我一​​个错误:

org.hibernate.PropertyNotFoundException: Could not find setter for companycode on class com.loansdb.data.Tbcompany

这个查询:

Query q;
q = session.createQuery("SELECT a.id.companycode,a.id.companyname,a.hcompanycode as hcompanycode FROM 
Tbcompany a");
q.setResultTransformer(Transformers.aliasToBean(Tbcompany.class));
list = q.list();

给了我这个错误:

org.hibernate.PropertyNotFoundException: Could not find setter for 0 on class com.loansdb.data.Tbcompany

有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

aliasToBean转换器使用返回的SQL列的名称,并尝试在具有{的类目标上查找具有同名的字段已创建{1}}方法。

因此,您的查询会返回set 公司代码

a.id.

错误说:

SELECT a.id.companycode as companycode,a.id.companyname as companyname,a.hcompanycode as hcompanycode FROM Tbcompany a

您的org.hibernate.PropertyNotFoundException: Could not find setter for companycode on class com.loansdb.data.Tbcompany 课程没有设置 companycode

因此,要更正您的Tbcompany课程并查看您的查询,我觉得这个课程类似于:

Tbcompany