我有一个hibernate查询来检查一个学生的别名(User_name)是唯一的,这很好用,这是我的代码。
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;
}这就是像这样创建一个select。
select count(*) as y0_ from student this_ where (this_.username is not null and this_.username=?)这很好,但我想知道是否可以尝试像这样的select
select count(username) as y0_ from student this_ where (this_.username is not null and this_.username=?)这是比我的更好的方法吗?
有更好的吗?
我知道我正在检查属性是否为空。但是这可以用hibernate来实现吗?
select count(name)非常感谢。
发布于 2013-09-09 17:04:35
你试过这个吗?
Projections.projectionList().add(Projections.count("username");https://stackoverflow.com/questions/16126048
复制相似问题