如何访问Hibernate中的投影条件生成的列(伪列)?

时间:2014-08-18 12:03:02

标签: hibernate projection

这是返回6列的标准     ProjectionList projList = Projections.projectionList();

    projList.add(Projections.property("se.ticker"),"ticker");
    projList.add(Projections.property("cl.firstName"),"firstName");
    projList.add(Projections.property("cl.middleName"),"middleName");
    projList.add(Projections.property("tr.client"),"client");
    projList.add(Projections.sum("tr.cumulativeQty"),"cumulativeQty");
    projList.add(Projections.sum("tr.cumulativeBalance"),"cumulativeBalance");
    projList.add(Projections.groupProperty("tr.securityId"));

    Criteria criteria = createEntityCriteria(TransactionDetails.class, "tr")
            .createAlias("tr.client", "cl")
            .createAlias("tr.security", "se")
            .add(Restrictions.eq("cl.id", clientId))
            .setProjection(projList);




    return (List<TransactionDetails>) criteria.list();

如何访问列详细信息(带列的数据)。 我给了别名但它没用。 请建议我访问列数据的方法。

2 个答案:

答案 0 :(得分:0)

您有两个选择

1&gt;您将获得结果作为数组列表。你可以迭代它们并获得列。

List<Object[]> criteria = criteria.list();

for (Object[] result : results) {
    String ticker = (String)result[0];
    String firstName = (String)result[1];
    // other properties same way

}

2&gt;你可以指定结果转换器,休眠将完成其余的工作。您必须使用VO对象列映射投影中的列。

在您的情况下,您可以拥有如下的VO对象。

class TransactionDetailVO{
    String ticker;
    String firstName;
    String lastName;
    //other properties. these properties map to the alias in projection      projList.add(Projections.property("cl.firstName"),"firstName"). here it is `firstName`.
}


Criteria criteria = createEntityCriteria(TransactionDetails.class, "tr")
            .createAlias("tr.client", "cl")
            .createAlias("tr.security", "se")
            .add(Restrictions.eq("cl.id", clientId))
            .setProjection(projList)
            .setResultTransformer(Transformers.aliasToBean(TransactionDetailVO.class));


List criteria = criteria.list();

for (Object result : results) {
    TransactionDetailVO transactionDetailVO = (TransactionDetailVO)result;
    // access the properties.
}

Here就是一个例子。

答案 1 :(得分:0)

我使用Iterator获取数据。它现在正在工作。

transactionDetailsList = transactionDetailsDao.getClientHoldingsBySecurityIdClientId(securityId,clientId);

        List list = transactionDetailsList;
        Iterator it = list.iterator();
        if (!it.hasNext()) {
            System.out.println("No any data!");
        } else {
            while (it.hasNext()) {
                TransactionDetailsDto transactionDetailsDto = new TransactionDetailsDto();
                Object[] row = (Object[]) it.next();
                int count=0;
                for (int i = 0; i < row.length; i++) {

                    switch (i) {
                        case 0:
                            transactionDetailsDto.setClientMasterId(Long.valueOf(row[i].toString()));
                            Client client = clientDAO.load(transactionDetailsDto.getClientMasterId());
                            transactionDetailsDto.setClientDto(new ClientTransformer().transform(client));
                            break;
                                }
                      }

}