首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java hibernate criteria projections only 1列

java hibernate criteria projections only 1列
EN

Stack Overflow用户
提问于 2013-04-21 06:54:00
回答 1查看 3.2K关注 0票数 0

我有一个hibernate查询来检查一个学生的别名(User_name)是唯一的,这很好用,这是我的代码。

代码语言:javascript
复制
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。

代码语言:javascript
复制
select count(*) as y0_ from student this_ where (this_.username is not null and this_.username=?)

这很好,但我想知道是否可以尝试像这样的select

代码语言:javascript
复制
select count(username) as y0_ from student this_ where (this_.username is not null and this_.username=?)

这是比我的更好的方法吗?

有更好的吗?

我知道我正在检查属性是否为空。但是这可以用hibernate来实现吗?

代码语言:javascript
复制
select count(name)

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-09 17:04:35

你试过这个吗?

代码语言:javascript
复制
Projections.projectionList().add(Projections.count("username");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16126048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档