Could not find setter for 0 on class MyClass

3610 ワード

今日プログラムを変更するときは、hqlで1つのクラスのいくつかの属性をクエリーして別のクラスにカプセル化する必要があります.
コードは次のとおりです.
@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 the alias names.
The transformTuple method of AliasToBeanResultTransformer class uses the alias names to find the setter methods of your resultClass (StudentDto) :
次に、A l iasToBeanResultTransformerクラスの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 use select 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を指定し、指定した結果クラスの属性名と同じ名前でなければなりません.