List of usage examples for org.hibernate Criteria setFirstResult
public Criteria setFirstResult(int firstResult);
From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java
License:Open Source License
@SuppressWarnings("unchecked") @Override//from w ww . jav a 2s. co m public Collection<T> searchAssignedResources(List<Principal> principals, final String searchPattern, final int start, final int length, final ColumnSort[] sorting, CriteriaConfiguration... configs) { Criteria criteria = createCriteria(getResourceClass()); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); if (StringUtils.isNotBlank(searchPattern)) { criteria.add(Restrictions.ilike("name", searchPattern)); } for (CriteriaConfiguration c : configs) { c.configure(criteria); } criteria.add(Restrictions.eq("realm", principals.get(0).getRealm())); criteria = criteria.createCriteria("roles"); criteria.add(Restrictions.eq("allUsers", true)); Set<T> everyone = new HashSet<T>(criteria.list()); criteria = createCriteria(getResourceClass()); ProjectionList projList = Projections.projectionList(); projList.add(Projections.property("id")); projList.add(Projections.property("name")); criteria.setProjection(Projections.distinct(projList)); criteria.setFirstResult(start); criteria.setMaxResults(length); if (StringUtils.isNotBlank(searchPattern)) { criteria.add(Restrictions.ilike("name", searchPattern)); } for (CriteriaConfiguration c : configs) { c.configure(criteria); } for (ColumnSort sort : sorting) { criteria.addOrder(sort.getSort() == Sort.ASC ? Order.asc(sort.getColumn().getColumnName()) : Order.desc(sort.getColumn().getColumnName())); } criteria.add(Restrictions.eq("realm", principals.get(0).getRealm())); criteria = criteria.createCriteria("roles"); criteria.add(Restrictions.eq("allUsers", false)); criteria = criteria.createCriteria("principals"); List<Long> ids = new ArrayList<Long>(); for (Principal p : principals) { ids.add(p.getId()); } criteria.add(Restrictions.in("id", ids)); List<Object[]> results = (List<Object[]>) criteria.list(); if (results.size() > 0) { Long[] entityIds = new Long[results.size()]; int idx = 0; for (Object[] obj : results) { entityIds[idx++] = (Long) obj[0]; } criteria = createCriteria(getResourceClass()); criteria.add(Restrictions.in("id", entityIds)); everyone.addAll((List<T>) criteria.list()); } return everyone; }
From source file:com.hyzy.core.orm.hibernate.HibernateDao.java
License:Apache License
/** * ?Criteria,.//from www.jav a 2 s . c o m */ protected Criteria setPageParameterToCriteria(final Criteria c, final Page<T> page) { //Assert.isTrue(page.getPageSize() > 0, "Page Size must larger than zero"); if (page.getFirst() != -1 && page.getPageSize() != -1) { //hibernatefirstResult??0 c.setFirstResult(page.getFirst() - 1); c.setMaxResults(page.getPageSize()); } if (page.isOrderBySetted()) { String[] orderByArray = StringUtils.split(page.getOrderBy(), ','); String[] orderArray = StringUtils.split(page.getOrder(), ','); Assert.isTrue(orderByArray.length == orderArray.length, "???,????"); for (int i = 0; i < orderByArray.length; i++) { if (Page.ASC.equals(orderArray[i])) { c.addOrder(Order.asc(orderByArray[i])); } else { c.addOrder(Order.desc(orderByArray[i])); } } } return c; }
From source file:com.ignou.aadhar.dao.hibernate.BankDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset.//from w ww . j a v a 2 s.c om * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override public List<Map<String, Object>> getBanks(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<Bank> banks = null; List<Map<String, Object>> returnBanks = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Bank.class); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; /* Now there are only two fields which we can search here. */ if ("name".equals(searchField)) { criteria.add(Restrictions.ilike("name", searchValue)); } else if ("url".equals(searchField)) { criteria.add(Restrictions.ilike("url", searchValue)); } } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { banks = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { banks = criteria.list(); } /* Format this data before sending back for any other further usage */ for (Bank bankRecord : banks) { /* Create new map for current bank and add it to master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", bankRecord.getId()); returnMap.put("name", bankRecord.getName()); returnMap.put("url", bankRecord.getUrl()); returnMap.put("totalCount", totalCount); returnBanks.add(returnMap); } return returnBanks; }
From source file:com.ignou.aadhar.dao.hibernate.CityDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset./*from ww w.j ava2s.co m*/ * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override public List<Map<String, Object>> getCities(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<City> cities = null; List<Map<String, Object>> returnCities = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(City.class, "c") .createAlias("state", "s"); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; /* Now there are only two fields which we can search here. */ if ("city".equals(searchField)) { criteria.add(Restrictions.ilike("c.city", searchValue)); } else if ("state".equals(searchField)) { criteria.add(Restrictions.ilike("s.state", searchValue)); } } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* We don't want to load the entire objects. Just the names would * suffice. Setting projections for the same. */ //criteria.setProjection(Projections.property("id").as("id")); //criteria.setProjection(Projections.property("city").as("city")); //criteria.setProjection(Projections.property("state").as("state")); /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { cities = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { cities = criteria.list(); } /* Format this data before sending back for any other further usage */ for (City cityRecord : cities) { /* Create a new map for current city and add it to master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", cityRecord.getId()); returnMap.put("city", cityRecord.getCity()); returnMap.put("state", cityRecord.getState().getState()); returnMap.put("totalCount", totalCount); returnCities.add(returnMap); } return returnCities; }
From source file:com.ignou.aadhar.dao.hibernate.DistrictDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset.//from w w w. j a v a2 s.c o m * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override public List<Map<String, Object>> getDistricts(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<District> districts = null; List<Map<String, Object>> returnDistricts = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(District.class, "d") .createAlias("state", "s"); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; /* Now there are only two fields which we can search here. */ if ("district".equals(searchField)) { criteria.add(Restrictions.ilike("d.district", searchValue)); } else if ("state".equals(searchField)) { criteria.add(Restrictions.ilike("s.state", searchValue)); } } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { districts = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { districts = criteria.list(); } /* Format this data before sending back for any other further usage */ for (District districtRecord : districts) { /* Create new map for current district and add it to master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", districtRecord.getId()); returnMap.put("district", districtRecord.getDistrict()); returnMap.put("state", districtRecord.getState().getState()); returnMap.put("totalCount", totalCount); returnDistricts.add(returnMap); } return returnDistricts; }
From source file:com.ignou.aadhar.dao.hibernate.ServiceProviderDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset./*from w w w.j a va 2 s . co m*/ * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override public List<Map<String, Object>> getServiceProviders(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<ServiceProvider> records = null; List<Map<String, Object>> returnRecords = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(ServiceProvider.class); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; /* Set the matching field accordingly */ if ("name".equals(searchField)) { criteria.add(Restrictions.ilike("name", searchValue)); } else if ("requestUrl".equals(searchField)) { criteria.add(Restrictions.ilike("requestUrl", searchValue)); } else if ("responseUrl".equals(searchField)) { criteria.add(Restrictions.ilike("responseUrl", searchValue)); } else if ("accountNumber".equals(searchField)) { criteria.add(Restrictions.ilike("accountNumber", searchValue)); } else if ("bankIFSCode".equals(searchField)) { criteria.add(Restrictions.ilike("bankIFSCode", searchValue)); } } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { records = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { records = criteria.list(); } /* Format this data before sending back for any other further usage */ for (ServiceProvider serviceProvider : records) { /* Create new map for current service provider and add it to * master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", serviceProvider.getId()); returnMap.put("name", serviceProvider.getName()); returnMap.put("requestUrl", serviceProvider.getRequestUrl()); returnMap.put("responseUrl", serviceProvider.getResponseUrl()); returnMap.put("accountNumber", serviceProvider.getAccountNumber()); returnMap.put("bankIFSCCode", serviceProvider.getBankIFSCCode()); returnMap.put("totalCount", totalCount); returnRecords.add(returnMap); } return returnRecords; }
From source file:com.ignou.aadhar.dao.hibernate.StateDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset.// ww w . j ava 2 s . co m * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override public List<Map<String, Object>> getStates(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<State> states = null; List<Map<String, Object>> returnStates = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(State.class, "s"); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; criteria.add(Restrictions.ilike("s.state", searchValue)); } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { states = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { states = criteria.list(); } /* Format this data before sending back for any other further usage */ for (State stateRecord : states) { /* Create new map for current state and add it to master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", stateRecord.getId()); returnMap.put("state", stateRecord.getState()); returnMap.put("totalCount", totalCount); returnStates.add(returnMap); } return returnStates; }
From source file:com.ignou.aadhar.dao.hibernate.TransactionDaoHibernate.java
License:Open Source License
/** * Gets the records from the Database based on the parameters provided. * @param searchField The field name on which the search is to be made. * @param searchValue Value which needs to be searched. * @param pageNumber Initial offset of the records. * @param recordsPerPage Total number of records which are selected for * resultset.// ww w . java 2s. c o m * @param sortField Name of the field on which the data needs to be sorted. * @param sortOrder Order in which the sortField is sorted. * @return Returns the records as list of map where each map stores the * record data as key-value pairs. */ @Override @Transactional public List<Map<String, Object>> getTransactions(String searchField, String searchValue, Integer pageNumber, Integer recordsPerPage, String sortField, String sortOrder) { List<Transaction> records = null; List<Map<String, Object>> returnRecords = new ArrayList<Map<String, Object>>(); Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(Transaction.class); /* Add the search parameters to the criteria */ if (searchField != null && !searchField.isEmpty() && searchValue != null && !searchValue.isEmpty()) { /* Prefix and suffix the searchValue with % */ searchValue = "%" + searchValue + "%"; /* Set the matching field accordingly */ if ("uid".equals(searchField)) { criteria.add(Restrictions.ilike("citizen.uid", searchValue)); } else if ("serviceprovider".equals(searchField)) { criteria.add(Restrictions.ilike("serviceProvider.name", searchValue)); } else if ("clientTxnId".equals(searchField)) { criteria.add(Restrictions.ilike("spTransactionId", searchValue)); } else if ("bankTxnId".equals(searchField)) { criteria.add(Restrictions.ilike("bankTransactionId", searchValue)); } else if ("status".equals(searchField)) { criteria.add(Restrictions.ilike("status", searchValue)); } } /* Let's first get the total number of records that satisfy the provided * parameters. */ String totalCount = (String) criteria.setProjection(Projections.rowCount()).uniqueResult().toString(); /* Reset the Criteria specification to remove the projection details */ criteria.setProjection(null); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); /* Set the default sort field if not provided */ if (sortField != null && !sortField.isEmpty()) { /* Check what order was provided for this field */ if ("asc".equals(sortOrder)) { /* Sort in ascending order */ criteria.addOrder(Order.asc(sortField)); } else if ("desc".equals(sortOrder)) { /* Sort in descending order */ criteria.addOrder(Order.desc(sortField)); } else { /* Default sort behaviour other wise */ criteria.addOrder(Order.asc(sortField)); } } /* Set the record filtering on pageCount and recordsPerPage if they are * available. */ if (pageNumber != null && recordsPerPage != null) { records = criteria.setFirstResult(pageNumber).setMaxResults(recordsPerPage).list(); } else { records = criteria.list(); } /* Format the Date */ SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy h:m:s a"); /* Format this data before sending back for any other further usage */ for (Transaction transaction : records) { /* Create new map for current transaction and add it to * master list */ Map<String, Object> returnMap = new LinkedHashMap<String, Object>(); returnMap.put("id", transaction.getId()); returnMap.put("uid", transaction.getCitizen().getUid()); returnMap.put("name", transaction.getCitizen().getName()); returnMap.put("serviceprovider", transaction.getServiceProvider().getName()); returnMap.put("clientTxnId", transaction.getSpTransactionId()); returnMap.put("bankTxnId", transaction.getBankTransactionId()); returnMap.put("status", transaction.getStatus()); returnMap.put("created", format.format(transaction.getCreated())); returnMap.put("amount", transaction.getAmount()); returnMap.put("totalCount", totalCount); returnRecords.add(returnMap); } return returnRecords; }
From source file:com.inet.base.ejb.business.BaseManageSessionBean.java
License:Open Source License
/** * Query and result the data from the given search persistence object. * * @param search the given persistence object used to build the criteria. * @param startAt the start at item the user want to retrieve. * @param maxItems the max items the user want to retrieve. * * @return the list of data that match the given criteria. * * @throws EjbException if we could not build and execute query to retrieve objects from the * container./*from w w w . j a v a 2 s .com*/ */ @SuppressWarnings({ "unchecked" }) public List<T> query(final T search, final int startAt, final int maxItems) throws EjbException { try { // create criteria from the given persistence object. final Criteria criteria = buildQuery(search); // set the limit objects that user want to retrieve. criteria.setFirstResult(startAt); criteria.setMaxResults(maxItems); return criteria.list(); } catch (final HibernateException hbex) { final String msg = "Could not execute query to retrieve the objects from the container."; throw new EjbException(msg, hbex); } }
From source file:com.inet.base.ejb.business.BaseSessionBean.java
License:Open Source License
/** * Query and result the data from the given search persistence object. * * @param search the given persistence object used to build the criteria. * @param startAt the start at item the user want to retrieve. * @param maxItems the max items the user want to retrieve. * * @return the list of data that match the given criteria. * * @throws EjbException if we could not execute query to retrieve the matching data in container. *//*w w w. j av a 2s .c om*/ @SuppressWarnings({ "unchecked" }) @TransactionAttribute(TransactionAttributeType.SUPPORTS) public List<T> query(final T search, final int startAt, final int maxItems) throws EjbException { try { // create criteria from the given persistence object. final Criteria criteria = buildQuery(search); // set the limit objects that user want to retrieve. criteria.setFirstResult(startAt); criteria.setMaxResults(maxItems); return criteria.list(); } catch (final HibernateException hex) { final String msg = "Could not execute query to retrieve the matching data in container."; throw new EjbException(msg, hex); } }