我正在尝试使用投影从表中只获取一列(countColumn)。但不知何故,它将返回完整的数据库行(表示表中唯一记录的所有列)。请帮我确定修复方法。提前谢谢。
代码:
final String queryCount = "from DomainObj objShortName where objShortName.field1=:field1";
HibernateCallback hibernateCallback = new HibernateCallback(){
public Object doInHibernate(Session session) {
Query query = session.createQuery(queryCount);
query.setParameter("field1", field1);
query.setMaxResults(1);
Criteria criteria = session.createCriteria(DomainObject.class, "domainObj");
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("objShortName.countColumn")); //countColumn is parameted defined in my domain bean which is for countColumn in database table.
criteria.setProjection(proList);
criteria.setResultTransformer(Transformers.aliasToBean(DomainObject.class));
return query.list();
}
};
答案 0 :(得分:0)
您无法将实体和项目字段选择回同一实体对象。您需要使用非实体DTO,它具有构造函数来获取预测值。
删除结果转换器:
criteria.setResultTransformer(Transformers.aliasToBean(DomainObject.class));
简单地提取这样的投影值:
List results = criteria.list();
for(Object result : results) {
Object[] data = (Object[]) result;
Number countColumn = data[0];
...
}