如何使用hibernate标准只返回对象的一个​​元素而不是整个对象? [英] How to use hibernate criteria to return only one element of an object instead the entire object?

查看:124
本文介绍了如何使用hibernate标准只返回对象的一个​​元素而不是整个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图仅获取对象bob的id列表,而不是bob列表。对于HQL请求可以,但是我会知道是否可以使用标准?

I'm trying to get only the list of id of object bob for example instead of the list of bob. It's ok with a HQL request, but I would know if it's possible using criteria ?

一个例子:

An example :

final StringBuilder hql = new StringBuilder();
hql.append( "select bob.id from " )
    .append( bob.class.getName() ).append( " bob " )
    .append( "where bob.id > 10");

final Query query = session.createQuery( hql.toString() );
return query.list();


推荐答案

我认为你可以用Projections来做到这一点,

I think you could do that with Projections, something like

Criteria.forClass(bob.class.getName())
        .add(Restrictions.gt("id", 10))
        .setProjection(Projections.property("id"))
        );

这篇关于如何使用hibernate标准只返回对象的一个​​元素而不是整个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆