Example usage for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY

List of usage examples for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY

Introduction

In this page you can find the example usage for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY.

Prototype

ResultTransformer DISTINCT_ROOT_ENTITY

To view the source code for org.hibernate.criterion CriteriaSpecification DISTINCT_ROOT_ENTITY.

Click Source Link

Document

Each row of results is a distinct instance of the root entity

Usage

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w ww.j  av a  2 s.co  m*/
public T getResourceByIdAndPrincipals(Long resourceId, List<Principal> principals) {

    Set<Long> ids = new HashSet<Long>();
    for (Principal p : principals) {
        ids.add(p.getId());
    }
    Criteria crit = createCriteria(getResourceClass());
    crit.add(Restrictions.eq("id", resourceId));
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit = crit.createCriteria("roles");
    crit = crit.createCriteria("principals");
    crit.add(Restrictions.in("id", ids));

    return (T) crit.uniqueResult();
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w w  w . j a v  a  2  s .  c o m
public List<T> getResources(Realm realm) {

    Criteria crit = createCriteria(getResourceClass());
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.setFetchMode("roles", FetchMode.SELECT);
    crit.add(Restrictions.eq("deleted", false));
    crit.add(Restrictions.eq("realm", realm));

    return (List<T>) crit.list();
}

From source file:com.hypersocket.resource.AbstractAssignableResourceRepositoryImpl.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public List<T> allResources() {

    Criteria crit = createCriteria(getResourceClass());
    crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    crit.setFetchMode("roles", FetchMode.SELECT);
    crit.add(Restrictions.eq("deleted", false));

    return (List<T>) crit.list();
}

From source file:com.hypersocket.session.SessionRepositoryImpl.java

License:Open Source License

@Override
public Long getActiveSessionCount(boolean distinctUsers) {
    Criteria criteria = createCriteria(Session.class);

    criteria.add(Restrictions.isNull("signedOut"));
    criteria.add(Restrictions.eq("system", false));

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (distinctUsers) {
        criteria.setProjection(Projections.countDistinct("principal"));
    } else {/*from   w  w w.  j a  va2 s.  com*/
        criteria.setProjection(Projections.rowCount());
    }
    return (long) criteria.uniqueResult();
}

From source file:com.hypersocket.session.SessionRepositoryImpl.java

License:Open Source License

@Override
public Long getSessionCount(final Date startDate, final Date endDate, final boolean distinctUsers) {

    Criteria criteria = createCriteria(Session.class);

    criteria.add(Restrictions.or(//from   w  w  w  .  j av a 2 s .  c  o  m
            Restrictions.and(Restrictions.ge("created", startDate), Restrictions.lt("created", endDate)),
            Restrictions.and(Restrictions.lt("created", startDate), Restrictions
                    .or(Restrictions.ge("signedOut", startDate), Restrictions.isNull("signedOut")))));

    criteria.add(Restrictions.eq("system", false));

    criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    if (distinctUsers) {
        criteria.setProjection(Projections.countDistinct("principal"));
    } else {
        criteria.setProjection(Projections.rowCount());
    }
    return (long) criteria.uniqueResult();
}

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 ww w .  j a v a  2  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>> 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 w  ww  .  ja  va2  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>> 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   www  .j a  va2  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  ww w .  j av 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>> 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.//  w  w  w.j  a v  a 2s  .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>> 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;
}