i did it, the problem is that i want to page the result, (with limit ..)
with little calculations i make it ;)
about the union -> you can't make a that query
Quote:
select * from users where prop1 is not null order by prop1, laslogin asc
UNION
select * from users where prop1 is null order by prop1, laslogin asc
must be only one "order by"
about the join, i will get duplicate values, so..
i hope my solution to be helpful to someone :)
Code:
public int getUsersCount(){
return (Integer)hs.findByDetachedCriteria(DetachedCriteria.forClass(User.class).setProjection(Projections.rowCount())).get(0);
}
public int getNotRegisteredUsersCount(){
return (Integer)hs.findByDetachedCriteria(DetachedCriteria.forClass(User.class).add(Expression.isNotNull("uinfo")).setProjection(Projections.rowCount())).get(0);
}
public List getUser(int first, int max, String orderBy, String orderDirection){
List result, result2 = null;
DetachedCriteria criteria, criteria2 = null;
if(orderDirection.equals("asc"))
criteria = DetachedCriteria.forClass(User.class).add(Expression.isNotNull("uinfo")).addOrder(Order.asc(orderBy));
else
criteria = DetachedCriteria.forClass(User.class).add(Expression.isNotNull("uinfo")).addOrder(Order.desc(orderBy));
hs.setFirstResult(criteria, first);
hs.setMaxResults(criteria, max);
result = hs.findByDetachedCriteria(criteria);
log.debug("result size criteria1 = " + result.size());
if(result.size() < max){
if(orderDirection.equals("asc"))
criteria2 = DetachedCriteria.forClass(User.class).add(Expression.isNull("uinfo")).addOrder(Order.asc(orderBy));
else
criteria2 = DetachedCriteria.forClass(User.class).add(Expression.isNull("uinfo")).addOrder(Order.desc(orderBy));
if(!result.isEmpty()){
int max2 = max - result.size();
log.debug("result is not empty! first result = 0, max results = " + max2);
hs.setFirstResult(criteria2, 0);
hs.setMaxResults(criteria2, max2);
}
else{
int first2 = first - getNotRegisteredUsersCount();
log.debug("result is empty! first result = " + first2 + ", max results = " + max);
hs.setFirstResult(criteria2, first2);
hs.setMaxResults(criteria2, max);
}
result2 = hs.findByDetachedCriteria(criteria2);
log.debug("result2 size = " + result2.size());
result.addAll(result2);
}
log.debug("result size last = " + result.size());
return result;
}