我试图只获取对象bob的id列表,而不是bob列表.HQL请求没问题,但我知道是否可以使用标准?
一个例子 :
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();
agnul.. 45
我认为你可以用预测做到这一点,比如说
Criteria.forClass(bob.class.getName()) .add(Restrictions.gt("id", 10)) .setProjection(Projections.property("id")) );
korosmatick.. 18
同样你也可以:
Criteria criteria = session.createCriteria(bob.class); criteria.add(Expression.gt("id", 10)); criteria.setProjection(Projections.property("id")); criteria.addOrder(Order.asc("id")); return criteria.list();
小智.. 8
或setProjection(Projections.id())
我认为你可以用预测做到这一点,比如说
Criteria.forClass(bob.class.getName()) .add(Restrictions.gt("id", 10)) .setProjection(Projections.property("id")) );
同样你也可以:
Criteria criteria = session.createCriteria(bob.class); criteria.add(Expression.gt("id", 10)); criteria.setProjection(Projections.property("id")); criteria.addOrder(Order.asc("id")); return criteria.list();
或setProjection(Projections.id())