i have a hibernate query to check is the alias of a student(user_name) is unique this is working very well this is my code.
public Boolean isAliasUniqueInStudent(String alias)
{
Session session = getHibernateTemplate().getSessionFactory().openSession();
ProjectionList p = Projections.projectionList().add(Projections.rowCount());
org.hibernate.criterion.Conjunction exist=(Conjunction)Restrictions.conjunction().add(Restrictions.isNotNull("username")).add(Restrictions.eq("username",alias));
Criteria criteria = session().createCriteria(Student.class).setProjection(p).add(exist);
Long result=(Long)criteria.uniqueResult();
closeSession(session);
return result.intValue()==0;
}
this is creating a select like this.
select count(*) as y0_ from student this_ where (this_.username is not null and this_.username=?)
this is very good but i was wondering is possible try a select like
select count(username) as y0_ from student this_ where (this_.username is not null and this_.username=?)
is this a better approach than mine?
exist a better one?
i know i am checking the property not being null.. but is this possible doing it with hibernate.
select count(name)
thanks a lot.
Have you tried this?
Projections.projectionList().add(Projections.count("username");