今天修改程序的时候,需要通过hql将一个类的几个属性查询出来并封装到另外一个类中。

代码如下:


@SuppressWarnings("unchecked")
    public List<ApkInfo> listPushInfo(final UserPojo user) {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
            @Override
            public Object doInHibernate(Session session)
                    throws HibernateException, SQLException {
                String hql = "select aip.id,aip.userId,aip.channel,aip.name from AirpushApkInfoPojo aip where userId = '"
                        + user.getId() + "' and status <> '-1'";
                Query query = session.createQuery(hql).setResultTransformer(
                        Transformers.aliasToBean(ApkInfo.class));
                return query.list();
            }
        });
    }


很遗憾的出现了异常,异常信息如下:


org.springframework.orm.hibernate3.HibernateSystemException: Could not find setter for 0 on class cn.joy.pojo.ApkInfo; nested exception is org.hibernate.PropertyNotFoundException: Could not find setter for 0 on class cn.joy.pojo.ApkInfo


在网上搜了很多解决办法都没搞定,最后在stackoverflow上面看到有人和我出现了同样的问题,按照他的方法果然解决这个异常了。


As you are using AliasToBeanResultTransformer, you have to declare proper alias names in your query, as its name says that it will transform the results into your resultClass using thealias names.

The transformTuple method of AliasToBeanResultTransformer class uses the alias names to find the setter methods of your resultClass (StudentDto):

下面这个就是AliasToBeanResultTransformer类的transformTuple方法:

public Object transformTuple(Object[] tuple, String[] aliases) {
        Object result;
        try {
            if ( setters == null ) {
                setters = new Setter[aliases.length];
                for ( int i = 0; i < aliases.length; i++ ) {
                    String alias = aliases[i];
                    if ( alias != null ) {
                        setters[i] = propertyAccessor.getSetter( resultClass, alias );
                    }
                }
            }
            result = resultClass.newInstance();
            for ( int i = 0; i < aliases.length; i++ ) {
                if ( setters[i] != null ) {
                    setters[i].set( result, tuple[i], null );
                }
            }
        }
        catch ( InstantiationException e ) {
            throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
        }
        catch ( IllegalAccessException e ) {
            throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
        }
        return result;
    }

In order to make AliasToBeanResultTransformer work properly you need to use proper alias names in your query. If the property name in your StudentDto class is name, you should useselect student.name as name; using select student.name as n will again throw exception. The alias names that you are using in the query should be same as the property names in your DTO class.



解决办法就是必须要为查询的字段指定alias name,而且该name必须和指定的结果类的属性名一样。