0

I have a method like below

public List<String> getSimilarResourceNames(String resourceName){
    String searchString = "%"+resourceName+"%";
    Session session = getSession();
    Criteria criteria = session.createCriteria(Resource.class);
    criteria.add(Restrictions.like("name", searchString));
    return  criteria.list()
}

This will return me the entire resource from the DB, but what i need is just the name of the resource. How can I accomplish that ?

Tiny
  • 27,221
  • 105
  • 339
  • 599
robin
  • 1,893
  • 1
  • 18
  • 38

2 Answers2

3

Use Projection, you can find examples in Hibernate documentation.

Criteria criteria = session.createCriteria(Resource.class);
criteria.setProjection(Property.forName("name"))
criteria.add(Restrictions.like("name", searchString));
Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
2

By using Projection you will get other fields (Which you did not got by Projection) in your Pojo setted to default values. In HQL you can get specified column values as follow:

Query query = session.createQuery("select u.fullname from Users u");
List<Object[]> rows = query.list();
List<String> fullnames = new ArrayList<String>();
for (Object[] row: rows) {
    fullnames.add(row[0]);
}

I hope this will help you.

Afsun Khammadli
  • 2,048
  • 4
  • 18
  • 28