休眠addScalar和setResultTransformer的问题

时间:2018-09-28 03:37:29

标签: hibernate

在hibernate3中,我的代码如下:

StringBuffer stringBuffer=new StringBuffer();
    stringBuffer.append("SELECT a.contract_id AS contractId,a.phone AS phone");
    stringBuffer.append(" FROM t_user_contract a WHERE a.user_id=? ORDER BY a.create_date DESC");
    SQLQuery query= getSession().createSQLQuery(stringBuffer.toString());
    query.setParameter(0, userId);
    query.addScalar("contractId", Hibernate.STRING);
    query.addScalar("phone", Hibernate.STRING);

    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 
    return query.list();

当我删除下面的代码时,会发生错误

query.addScalar("contractId", Hibernate.STRING);
query.addScalar("phone", Hibernate.STRING);

但是在hibernate4中,我下面的代码还可以:

StringBuffer stringBuffer=new StringBuffer();
    stringBuffer.append("SELECT a.contract_id AS contractId,a.phone AS phone");
    stringBuffer.append(" FROM t_user_contract a WHERE a.user_id=? ORDER BY a.create_date DESC");
    SQLQuery query= getSession().createSQLQuery(stringBuffer.toString());
    query.setParameter(0, userId);
    query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); 
    return query.list();

如何删除hibernate3中下面的代码,如您所知,如果列太多,query.addScalar也太多,将浪费很多时间,非常感谢

query.addScalar("contractId", Hibernate.STRING);
    query.addScalar("phone", Hibernate.STRING);

0 个答案:

没有答案