Hibernate select子句不起作用

时间:2013-11-27 14:44:40

标签: hibernate

我有一个遗留数量很多列的遗留表,我试图限制hibernate填充的列数。如果我只是从“from”子句运行到最后,它工作正常,加载每一列,但当我添加“选择a.col1,a.col2 ...”等时,我得到一个对象数组而不是bean的实例。

我尝试添加结果转换器,但似乎认为第一列名为“0”。我想结果变换器进入图片时已经打破了。我想知道是否有案例不匹配,所以我把所有内容都改成大写,但这没有帮助。

更新: 这是SQL:

Select a.SZ_ABND_NO, a.ASSET_VAL, a.FO_TYP, a.ASSET_ID, a.AGCY_RGN_CD, a.LIT_AGCY_CD, a.ASSET_TYP, a.SZ_AGCY_CD, a.SUB_OFC_ID, a.OFC_ID, a.CA_ID_AGCY, a.PROC_DIST from FlatAssetT a where a.ASSET_ID in (:assetList) order by a.ASSET_ID

豆子很大。这12列是数百个(是复数)。我会给出一个选定的豆片。

@Entity
@Table(name = "FLATASSET_T", schema="K702PRDR")
public class FlatAssetT {
    @Column(name="ADPT_DT", nullable=false)
    private Date ADPT_DT;

    @Id
    @Column(name="ASSET_ID", nullable=false)
    private String ASSET_ID;

    @Column(name="ASSET_ID_TYP", nullable=false)
    private String ASSET_ID_TYP;

    @Column(name="SZ_ABND_NO", nullable=false)
    private String SZ_ABND_NO;
[etc.]
    public String getSZ_ABND_NO() {
        return SZ_ABND_NO;
    }
    public void  setSZ_ABND_NO(String arg) {
        this.SZ_ABND_NO=arg;
    }
    public String getASSET_ID() {
        return ASSET_ID;
    }
    public void  setASSET_ID(String arg) {
        this.ASSET_ID=arg;
    }
    public Date getADPT_DT() {
        return ADPT_DT;
    }
    public void  setADPT_DT(Date arg) {
        this.ADPT_DT=arg;
    }
    public String getASSET_ID_TYP() {
        return ASSET_ID_TYP;
    }
    public void  setASSET_ID_TYP(String arg) {
        this.ASSET_ID_TYP=arg;
    }
[etc.]
}

2 个答案:

答案 0 :(得分:0)

除了bean的默认构造函数之外,还有一个自定义构造函数,它使用有限的参数集(与从DB中获取的参数匹配),然后使用'select new MyBean(..)...'语法。有关更详细的说明,请参阅以下链接。

New object with HQL

此外,您可以为所选的每列提供别名,然后填充地图,如

所示

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html#queryhql-select

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat

可能是你可以为你的bean引入一个构造函数来接受一个地图然后使用它(加入这两种方法)

答案 1 :(得分:0)

您可以使用此方法从此article

查询它

它使用Apache BeanUtils。作者将参数作为“家庭作业”留给了读者。

本质上,查询数据并迭代结果,创建bean的新实例并为其设置可用数据,使用bean创建新列表。

public List find(final String hqlQuery) throws Exception {

    List results = new ArrayList();
    Query query = SessionManager.currentSession().createQuery(hqlQuery);
    Type beanType = query.getReturnTypes()[0];
    Class beanClass = beanType.getReturnedClass();
    String[] columns = extractColumns(hqlQuery);
    String[] attributeNames = getAttributeFieldNames(columns);
    String[] resultFieldNames = getResultFieldNames(columns);
    Iterator iter = query.iterate();
    while(iter.hasNext()) {
        Object[] row = (Object[]) iter.next();
        Object bean = beanClass.newInstance();
        for (int j = 0; j < row.length; j++) {
            if (row[j] != null) {
                initialisePath(bean, attributeNames[j]);
                PropertyUtils.setProperty(bean, attributeNames[j], row[j]);
            }
        }
        results.add(bean);
    }
    return results;
    }

    private static void initialisePath(final Object bean, final String fieldName) throws Exception {
            int dot = fieldName.indexOf('.');
            while (dot >= 0) {
                String attributeName = fieldName.substring(0, dot);
                Class attributeClass = PropertyUtils.getPropertyType(bean, attributeName);
                if (PropertyUtils.getProperty(bean, attributeName) == null) {
                    PropertyUtils.setProperty(bean, attributeName, attributeClass.newInstance());
            }
            dot = fieldName.indexOf('.', dot + 1);
            }
    }