Stanley Mungai
Stanley Mungai

Reputation: 4150

Return distinct Criteria result as String in Hibernate

I am trying to Create a criteria that will select distinct values from a column. How can I return this value as String;

I have this:

Criteria criteria = 
    session.createCriteria(Students.class)
           .add(Restrictions.eq("studentYear","2"))
           .add(Restrictions.eq("studentSemester","1"))
           .setProjection(Projections.distinct(Projections.property("studentHostel")));

any ideas will be appreciated. Can I convert this to a String value?

Upvotes: 0

Views: 1820

Answers (1)

Ketan Bhavsar
Ketan Bhavsar

Reputation: 5396

I think you have to do it manually like below.

Criteria criteria = 
    session.createCriteria(Students.class)
           .add(Restrictions.eq("studentYear","2"))
           .add(Restrictions.eq("studentSemester","1"))
           .setProjection(Projections.distinct(Projections.property("studentHostel")));

List<String> listObj = criteria.list();
StringBuffer sb = new StringBuffer();
for(String str: listObj){
 sb.append(str+',');
}

Upvotes: 1

Related Questions