<p>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 ?</p>

<p>An example :</p>

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

final Query query = session.createQuery( hql.toString() );
return query.list();
</code></pre>

[{'answer_id': 58624, 'author': 'agnul', 'author_id': 6069, 'author_profile': 'https://Stackoverflow.com/users/6069', 'pm_score': 6, 'selected': False, 'text': '<p>I think you could do that with Projections, something like</p>\n\n<pre><code>Criteria.forClass(bob.class.getName())\n        .add(Restrictions.gt("id", 10))\n        .setProjection(Projections.property("id"))\n        );\n</code></pre>\n'}, {'answer_id': 59028, 'author': 'jspinks', 'author_id': 5253, 'author_profile': 'https://Stackoverflow.com/users/5253', 'pm_score': 3, 'selected': False, 'text': '<p>or setProjection(Projections.id())</p>\n'}, {'answer_id': 781235, 'author': 'Avinash', 'author_id': 42791, 'author_profile': 'https://Stackoverflow.com/users/42791', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.devarticles.com/c/a/Java/Hibernate-Criteria-Queries-in-Depth/2/" rel="nofollow noreferrer">http://www.devarticles.com/c/a/Java/Hibernate-Criteria-Queries-in-Depth/2/</a></p>\n'}, {'answer_id': 17128402, 'author': 'korosmatick', 'author_id': 1885786, 'author_profile': 'https://Stackoverflow.com/users/1885786', 'pm_score': 4, 'selected': False, 'text': '<p>Similarly you can also:</p>\n\n<pre><code>Criteria criteria = session.createCriteria(bob.class);\n\ncriteria.add(Expression.gt("id", 10));\n\ncriteria.setProjection(Projections.property("id"));\n\ncriteria.addOrder(Order.asc("id"));\n\nreturn criteria.list();\n</code></pre>\n'}, {'answer_id': 29683153, 'author': 'rogerdpack', 'author_id': 32453, 'author_profile': 'https://Stackoverflow.com/users/32453', 'pm_score': 1, 'selected': False, 'text': '<p>Another option (though a bit un hibernate-esque) is to use "raw" sql, like this:</p>\n\n<pre><code>List&lt;Long&gt; myList = session.createSQLQuery("select single_column from table_name")\n          .addScalar("single_column", StandardBasicTypes.LONG).list();\n</code></pre>\n'}, {'answer_id': 37202954, 'author': 'Nalaka Dissanayake', 'author_id': 3650354, 'author_profile': 'https://Stackoverflow.com/users/3650354', 'pm_score': -1, 'selected': False, 'text': '<p>You can do that like this</p>\n\n<pre><code>    bob bb=null;\n\n    Criteria criteria = session.createCriteria(bob.class);  \n    criteria.add(Restrictions.eq("id",id));\n\n    bb = (bob) criteria.uniqueResult();\n</code></pre>\n\n<p>as Restrictions you can add your condition</p>\n'}, {'answer_id': 57584281, 'author': 'Ajinz', 'author_id': 11607606, 'author_profile': 'https://Stackoverflow.com/users/11607606', 'pm_score': 2, 'selected': False, 'text': '<pre><code>SessionFactory sessionFactory;    \nCriteria crit=sessionFactory.getCurrentSession().createCriteria(Model.class);\ncrit.setProjection(Projections.property("id"));\nList result = crit.list();\n</code></pre>\n\n<p>This code code will give you list of ids in the model class like <code>[1,2,3]</code>.\nif you wants to get the array list like <code>[{"id":1},{"id":2}]</code> then use the following code</p>\n\n<pre><code>SessionFactory sessionFactory;    \nCriteria crit=sessionFactory.getCurrentSession().createCriteria(Model.class); \ncrit.setProjection(Projections.property("id").as("id")); \nList result = crit.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/58561', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']