Example usage for org.hibernate Criteria setMaxResults

List of usage examples for org.hibernate Criteria setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Criteria setMaxResults.

Prototype

public Criteria setMaxResults(int maxResults);

Source Link

Document

Set a limit upon the number of objects to be retrieved.

Usage

From source file:com.lyh.licenseworkflow.dao.EnhancedHibernateDaoSupport.java

/**
 * ?SQLor?? from DeviceResource where (type = 'ROUTER' or type = 'L3_SWITCH');
 * <p/>/*from  w w w.  j a v a2  s.  c o m*/
 * <p/>
 * Notes: or<br>
 * <br>
 * Create Author : allen.wang <br>
 * Create Date : 2009-3-18
 *
 * @param start
 * @param pageSize
 * @param propertyName
 * @param values
 * @return
 */
@SuppressWarnings("unchecked")
public List<T> getPagingEntitiesByOrCondition(final int start, final int pageSize, final String propertyName,
        final Object... values) {
    return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = session.createCriteria(getEntityName());
            Disjunction d = Restrictions.disjunction();
            criteria = criteria.add(d);
            for (int i = 0; i < values.length; i++)
                d.add(Restrictions.eq(propertyName, values[i]));
            criteria.setFirstResult(start);
            criteria.setMaxResults(pageSize);
            return criteria.list();
        }
    });
}

From source file:com.lyh.licenseworkflow.dao.EnhancedHibernateDaoSupport.java

/**
 * ?? from DeviceResource where type = 'ROUTER' and temp = false;
 *
 * @param start         ?//from w ww. ja va 2  s  .c om
 * @param pageSize      ??
 * @param propertyNames ???
 * @param values        
 * @return ??
 */
@SuppressWarnings("unchecked")
public List<T> getPagingEntitiesByPropNames(final int start, final int pageSize, final String[] propertyNames,
        final Object[] values) {
    if (ArrayUtils.isEmpty(propertyNames) || ArrayUtils.isEmpty(values)
            || propertyNames.length != values.length) {
        throw new IllegalArgumentException("arguments is invalid.");
    }
    return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {

        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria c = session.createCriteria(getEntityName());
            c.setFirstResult(start);
            c.setMaxResults(pageSize);
            for (int i = 0; i < propertyNames.length; i++) {
                String propertyName = propertyNames[i];
                if (propertyName.indexOf(".") != -1) {
                    Criteria subC = null;
                    String[] props = StringUtils.split(propertyName, ".");
                    for (int j = 0; j < props.length; j++) {
                        if (j != props.length - 1) {
                            subC = c.createCriteria(props[j]);
                        }
                    }
                    if (values[i] == null)
                        subC.add(Restrictions.isNull(propertyName.substring(propertyName.indexOf(".") + 1)));
                    else
                        subC.add(Restrictions.eq(propertyName.substring(propertyName.indexOf(".") + 1),
                                values[i]));
                } else {
                    if (values[i] == null)
                        c.add(Restrictions.isNull(propertyNames[i]));
                    else if (values[i].toString().indexOf("%") != -1) {// ,?value?%
                        c.add(Restrictions.like(propertyNames[i], values[i]));
                    } else {
                        c.add(Restrictions.eq(propertyNames[i], values[i]));
                    }
                }
            }
            return c.list();
        }
    });
}

From source file:com.mac.green_leaves.v1.master.MasterRepository.java

public List<Model> list(String keyword, Integer pageNumber, Integer branch, Class modelClass) {
    Session session = getSession();/*  w  w  w.  j a v  a  2s  .  c o  m*/

    Criteria criteria = session.createCriteria(modelClass);

    if (keyword != null) {
        Field[] fields = modelClass.getDeclaredFields();
        ArrayList<Criterion> criterions = new ArrayList<>();
        for (Field field : fields) {
            if (field.getType().isAssignableFrom(String.class)) {
                criterions.add(Restrictions.ilike(field.getName(), keyword, MatchMode.ANYWHERE));
            }
        }
        criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    }

    criteria.add(Restrictions.eq("branch", branch));

    if (pageNumber != null) {
        criteria.setFirstResult(PAGE_SIZE * (pageNumber - 1));
        criteria.setMaxResults(PAGE_SIZE);
    }

    return criteria.list();
}

From source file:com.matel.pg.dao.impl.AppInfoDAOImpl.java

@Override
public AppInfo getFirst() {

    AppInfo appInfo = null;//from   w  w w .j a v a  2 s.c  o m

    try {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(AppInfo.class);
        criteria.setMaxResults(1);
        appInfo = (AppInfo) criteria.uniqueResult();
    } catch (Exception e) {
    }
    return appInfo;
}

From source file:com.matel.pg.dao.impl.CompanyDAOImpl.java

@Override
public Company findByRequestToken(String requestToken) {
    Company company = null;// ww  w  .  j  a v a2s .co m

    try {
        Session session = sessionFactory.getCurrentSession();
        Criteria criteria = session.createCriteria(Company.class);
        criteria.add(Restrictions.eq("requestToken", requestToken));
        criteria.setMaxResults(1);
        company = (Company) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return company;
}

From source file:com.maydesk.base.dao.DaoRole.java

License:Mozilla Public License

public static boolean hasUserRole(MUser user, IRole role, MBase context) {
    Session session = PDHibernateFactory.getSession();
    Criteria criteria = session.createCriteria(MUserRole.class);
    criteria.add(eq("userRef", user));
    criteria.add(eq("roleName", role.name()));
    criteria.add(eq("roleClass", role.getClass().getCanonicalName()));
    criteria.add(eq("contextId", context.getId()));
    criteria.add(eq("contextClass", context.getClass()));
    criteria.setMaxResults(1);
    return criteria.list().size() > 0;

}

From source file:com.maydesk.base.dao.DaoRole.java

License:Mozilla Public License

public static boolean hasUserRole(MUser user, MBase context) {
    Session session = PDHibernateFactory.getSession();
    Criteria criteria = session.createCriteria(MUserRole.class);
    criteria.add(eq("userRef", user));
    criteria.add(eq("contextId", context.getId()));
    criteria.add(eq("contextClass", context.getClass().getCanonicalName()));
    criteria.setMaxResults(1);
    return criteria.list().size() > 0;
}

From source file:com.maydesk.base.dao.DaoUser.java

License:Mozilla Public License

public static List<MActivity> findActivities(MUser user) {
    Criteria criteria = PDHibernateFactory.getSession().createCriteria(MActivity.class);
    criteria.add(Restrictions.eq("userId", user.getId()));
    criteria.addOrder(Order.desc("date"));
    criteria.setMaxResults(100);
    return criteria.list();
}

From source file:com.maydesk.base.gui.user.DlgSelectUser.java

License:Mozilla Public License

private void readFromModel() {
    Criteria criteria = PDHibernateFactory.getSession().createCriteria(MUser.class);
    boolean hasRestriction = false;
    if (!PDUtil.isEmpty(txtLastName.getText())) {
        criteria.add(Restrictions.like("lastName", txtLastName.getText().trim() + "%")); //$NON-NLS-1$ //$NON-NLS-2$
        hasRestriction = true;//  w w  w  .j  a v  a 2s .  c  o  m
    }
    if (!PDUtil.isEmpty(txtFirstName.getText())) {
        criteria.add(Restrictions.like("firstName", txtFirstName.getText().trim() + "%")); //$NON-NLS-1$ //$NON-NLS-2$
        hasRestriction = true;
    }
    if (!PDUtil.isEmpty(txtLogin.getText())) {
        criteria.add(Restrictions.like("login", txtLogin.getText().trim() + "%")); //$NON-NLS-1$ //$NON-NLS-2$
        hasRestriction = true;
    }
    if (!hasRestriction) {
        criteria.add(Restrictions.isNotNull("lastName")); //$NON-NLS-1$
        criteria.add(Restrictions.not(Restrictions.eq("lastName", ""))); //$NON-NLS-1$ //$NON-NLS-2$
    }
    criteria.addOrder(Order.asc("lastName")); //$NON-NLS-1$
    criteria.addOrder(Order.asc("firstName")); //$NON-NLS-1$
    criteria.setMaxResults(20);
    List<MUser> users = criteria.list();
    tableModel.setValues(users);
    tableModel.fireTableDataChanged();
    if (users.size() > 0) {
        table.getSelectionModel().setSelectedIndex(0, true);
    }
}

From source file:com.maydesk.base.PDUserSession.java

License:Mozilla Public License

private Chat findLastChat(String otherUserId) {
    ChatManager chatmanager = connection.getChatManager();
    String threadId = user.getJabberId() + "|" + otherUserId + "|";
    Calendar calOneHourBefore = Calendar.getInstance();
    calOneHourBefore.add(Calendar.MINUTE, -60);

    Criteria c = PDHibernateFactory.getSession().createCriteria(MChatHistory.class);
    c.add(Restrictions.eq("user", user));
    c.add(Restrictions.eq("otherUser", otherUserId));
    c.add(Restrictions.ge("lastActivity", calOneHourBefore.getTime()));
    c.addOrder(Order.desc("lastActivity"));
    c.setMaxResults(1);
    MChatHistory chatHistory = (MChatHistory) c.uniqueResult();
    Chat chat = null;//  w w  w . j a va2s.c  o  m
    if (chatHistory != null) {
        chat = chatmanager.getThreadChat(threadId + chatHistory.getId());
        if (chat != null) {
            return chat;
        }
    }
    chatHistory = new MChatHistory();
    chatHistory.setUser(user);
    chatHistory.setOtherUser(otherUserId);
    chatHistory.setLastActivity(new Date());
    PDHibernateFactory.getSession().save(chatHistory);

    return chatmanager.createChat(otherUserId, threadId + chatHistory.getId(), null);

}