[英]using hibernate Criteria.uniqueResult() when there are several results
I have a table USERS with names and creation dates, and an api function 我有一个具有名称和创建日期的表USERS,以及一个api函数
T read(Criterion... criteria)
that searches by criterions that i cant change. 通过我无法更改的标准进行搜索。
My problem is that the function returns crit.uniqueResult() but sometimes the criteria gives many names (in which case i want only the name with the latest date). 我的问题是该函数返回crit.uniqueResult(),但有时条件会给出许多名称(在这种情况下,我只需要具有最新日期的名称)。 how can i add a criterion to make sure only the latest name is returned?
如何添加标准以确保仅返回最新名称?
public T read(Criterion... criteria){
Criteria crit = getSession(false).createCriteria(this.type);
for (Criterion c : criteria)
{
crit.add(c);
}
T entity = (T)crit.uniqueResult();
return entity;
}
Maybe you can have a read method that returned the top result, a method that returns a List<T>
and one that you pass in the number of results to return. 也许您可以有一个read方法返回最前面的结果,一个方法返回
List<T>
然后传递一个要返回的结果数。
For example you could use then use max results setting 例如,您可以先使用“最大结果”设置
criteria.setMaxResults(1);
List<T> results = criteria.list();
return results.get(0);
http://docs.jboss.org/hibernate/core/3.2/api/org/hibernate/Criteria.html#setMaxResults%28int%29 http://docs.jboss.org/hibernate/core/3.2/api/org/hibernate/Criteria.html#setMaxResults%28int%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.