Example usage for org.hibernate SQLQuery uniqueResult

List of usage examples for org.hibernate SQLQuery uniqueResult

Introduction

In this page you can find the example usage for org.hibernate SQLQuery uniqueResult.

Prototype

R uniqueResult();

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

/**
 * Gets the marker names by gids./*from  ww w .j  av  a  2s.  c  o  m*/
 * <p/>
 * - Searches the allele_values, char_values, mapping_pop_values tables for the existence of gids. - Gets marker ids from allele_values,
 * char_values, mapping_pop_values by gids - Gets marker name from marker table by marker ids - Returns marker names matching the gids
 *
 * @param gIds the g ids
 * @return the marker names by g ids
 * @ the MiddlewareQueryException
 */
public List<MarkerNameElement> getMarkerNamesByGIds(final List<Integer> gIds) {
    final List<MarkerNameElement> dataValues = new ArrayList<>();

    if (gIds == null || gIds.isEmpty()) {
        return dataValues;
    }

    try {
        // Search the allele_values, char_values, mapping_pop_values tables for the existence of gids.
        // by getting alleleCount, charCount and mappingCount

        SQLQuery query = this.getSession().createSQLQuery(AlleleValuesDAO.GET_ALLELE_COUNT_BY_GID);
        query.setParameterList("gIdList", gIds);
        final BigInteger alleleCount = (BigInteger) query.uniqueResult();

        query = this.getSession().createSQLQuery(CharValuesDAO.GET_CHAR_COUNT_BY_GID);
        query.setParameterList("gIdList", gIds);
        final BigInteger charCount = (BigInteger) query.uniqueResult();

        query = this.getSession().createSQLQuery(MappingPopValuesDAO.GET_MAPPING_COUNT_BY_GID);
        query.setParameterList("gIdList", gIds);
        final BigInteger mappingCount = (BigInteger) query.uniqueResult();

        // Retrieves markers that are being genotyped
        if (alleleCount.intValue() > 0) {
            query = this.getSession().createSQLQuery(MarkerDAO.GET_ALLELE_MARKER_NAMES_BY_GID);
            query.setParameterList("gIdList", gIds);
            final List results = query.list();
            dataValues.addAll(this.createMarkerNameElementList(results));
        }

        if (charCount.intValue() > 0) {
            query = this.getSession().createSQLQuery(MarkerDAO.GET_CHAR_MARKER_NAMES_BY_GID);
            query.setParameterList("gIdList", gIds);
            final List results = query.list();
            dataValues.addAll(this.createMarkerNameElementList(results));
        }

        if (mappingCount.intValue() > 0) {
            query = this.getSession().createSQLQuery(MarkerDAO.GET_MAPPING_MARKER_NAMES_BY_GID);
            query.setParameterList("gIdList", gIds);
            final List results = query.list();
            dataValues.addAll(this.createMarkerNameElementList(results));
        }

        return dataValues;

    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with getMarkerNamesByGIds(gIds=" + gIds + ") query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

/**
 * Gets the germplasm names by marker names.
 *
 * @param markerNames the marker names/*from  ww  w .ja va  2s  . co  m*/
 * @return the germplasm names by marker names
 * @ the MiddlewareQueryException
 */
public List<GermplasmMarkerElement> getGermplasmNamesByMarkerNames(final List<String> markerNames) {

    final ArrayList<GermplasmMarkerElement> dataValues = new ArrayList<>();

    if (markerNames == null || markerNames.isEmpty()) {
        return dataValues;
    }

    // Get marker_ids by marker_names
    final List<Integer> markerIds = this.getIdsByNames(markerNames, 0,
            Long.valueOf(this.countAll()).intValue());

    if (markerIds.isEmpty()) {
        return dataValues;
    }

    try {
        // Search the allele_values, char_values, mapping_pop_values tables for the existence of marker_ids.
        // by getting alleleCount, charCount and mappingCount

        SQLQuery query = this.getSession().createSQLQuery(AlleleValuesDAO.GET_ALLELE_COUNT_BY_MARKER_ID);
        query.setParameterList("markerIdList", markerIds);
        final BigInteger alleleCount = (BigInteger) query.uniqueResult();

        query = this.getSession().createSQLQuery(CharValuesDAO.GET_CHAR_COUNT_BY_MARKER_ID);
        query.setParameterList("markerIdList", markerIds);
        final BigInteger charCount = (BigInteger) query.uniqueResult();

        query = this.getSession().createSQLQuery(MappingPopValuesDAO.GET_MAPPING_COUNT_BY_MARKER_ID);
        query.setParameterList("markerIdList", markerIds);
        final BigInteger mappingCount = (BigInteger) query.uniqueResult();

        // Get marker name, germplasm name from allele_values given the marker names
        if (alleleCount.intValue() > 0) {
            query = this.getSession()
                    .createSQLQuery(AlleleValuesDAO.GET_ALLELE_GERMPLASM_NAME_AND_MARKER_NAME_BY_MARKER_NAMES);
            query.setParameterList("markerNameList", markerNames);
            final List results = query.list();
            dataValues.addAll(this.getGermplasmMarkerElementsFromList(results));
        }

        // Get marker name, germplasm name from char_values given the marker names
        if (charCount.intValue() > 0) {
            query = this.getSession()
                    .createSQLQuery(CharValuesDAO.GET_CHAR_GERMPLASM_NAME_AND_MARKER_NAME_BY_MARKER_NAMES);
            query.setParameterList("markerNameList", markerNames);
            final List results = query.list();
            dataValues.addAll(this.getGermplasmMarkerElementsFromList(results));
        }

        // Get marker name, germplasm name from mapping_pop_values given the marker names
        if (mappingCount.intValue() > 0) {
            query = this.getSession().createSQLQuery(
                    MappingPopValuesDAO.GET_MAPPING_GERMPLASM_NAME_AND_MARKER_NAME_BY_MARKER_NAMES);
            query.setParameterList("markerNameList", markerNames);
            final List results = query.list();
            dataValues.addAll(this.getGermplasmMarkerElementsFromList(results));
        }

        return dataValues;

    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException("Error with getGermplasmNamesByMarkerNames(markerNames="
                + markerNames + ") query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

/**
 * Count all marker types./* ww  w .  ja  v a2  s  .c  o m*/
 *
 * @return the long
 * @ the MiddlewareQueryException
 */
public long countAllMarkerTypes() {
    try {
        final SQLQuery query = this.getSession().createSQLQuery(MarkerDAO.COUNT_ALL_MARKER_TYPES);
        final BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        } else {
            return 0L;
        }
    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with countAllMarkerTypes() query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

/**
 * Count marker names by marker type.//from ww w .j av  a  2s  .  c  om
 *
 * @param markerType the marker type
 * @return the long
 * @ the MiddlewareQueryException
 */
public long countMarkerNamesByMarkerType(final String markerType) {
    try {
        final SQLQuery query = this.getSession().createSQLQuery(MarkerDAO.COUNT_MARKER_NAMES_BY_MARKER_TYPE);
        query.setParameter("markerType", markerType);
        final BigInteger result = (BigInteger) query.uniqueResult();

        if (result != null) {
            return result.longValue();
        }
        return 0;
    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException("Error with countMarkerNamesByMarkerType(markerType=" + markerType
                + ") query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

/**
 * Count all db accession ids.//from   w  w  w  .  j a  v a 2  s .  c  o  m
 *
 * @return the number of distinct db accession ids
 * @ the MiddlewareQueryException
 */
public long countAllDbAccessionIds() {
    try {
        final SQLQuery query = this.getSession().createSQLQuery(MarkerDAO.COUNT_ALL_DB_ACCESSION_IDS);
        final BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        } else {
            return 0L;
        }
    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with countAllDbAccessionIds() query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

public long countMarkersByIds(final List<Integer> markerIds) {
    if (markerIds == null || markerIds.isEmpty()) {
        return 0;
    }// w  ww . j av  a2  s  .c om
    try {
        final SQLQuery query = this.getSession().createSQLQuery(MarkerDAO.COUNT_MARKERS_BY_IDS);
        query.setParameterList("markerIdList", markerIds);
        final BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        } else {
            return 0L;
        }
    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with countMarkersByIds() query from Marker: " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

public long countMarkerIDsByMapIDAndLinkageBetweenStartPosition(final int mapID, final String linkageGroup,
        final double startPos, final double endPos) {
    try {/*from   w w w.  jav  a2s.  c  o  m*/

        final SQLQuery query;

        query = this.getSession()
                .createSQLQuery(MarkerDAO.COUNT_MARKER_IDS_BY_MAP_ID_AND_LINKAGE_BETWEEN_START_POSITION);
        query.setParameter("map_id", mapID);
        query.setParameter("linkage_group", linkageGroup);
        query.setParameter("start_position", startPos);
        query.setParameter("end_position", endPos);
        final BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        } else {
            return 0;
        }

    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException(
                "Error with countMarkerIdsByMapIDAndLinkageBetweenStartPosition(mapID=" + mapID
                        + ", linkageGroup=" + linkageGroup + ") query from Marker: " + e.getMessage(),
                e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerDAO.java

License:Open Source License

public Integer getIdByName(final String name) {
    try {//from ww w. j  a v a2  s.co m
        final SQLQuery query = this.getSession().createSQLQuery(MarkerDAO.GET_ID_BY_NAME);
        query.setParameter("markerName", name);
        return (Integer) query.uniqueResult();

    } catch (final HibernateException e) {
        LOG.error(e.getMessage(), e);
        throw new MiddlewareQueryException("Error with getIdByName(" + name + "): " + e.getMessage(), e);
    }
}

From source file:org.generationcp.middleware.dao.gdms.MarkerInfoDAO.java

License:Open Source License

/**
 * Count the number of entries in marker info corresponding to the given marker name.
 *
 * @param markerName the marker name/*ww w  .  j a  v a  2  s . c o m*/
 * @return the count
 * @throws MiddlewareQueryException
 */
public long countByMarkerName(String markerName) throws MiddlewareQueryException {

    if (markerName == null) {
        return 0;
    }

    try {
        SQLQuery query = this.getSession().createSQLQuery(MarkerInfoDAO.COUNT_BY_MARKER_NAME);
        query.setParameter("markerName", markerName);
        BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        }
        return 0;
    } catch (HibernateException e) {
        this.logAndThrowException("Error with countByMarkerName(markerName=" + markerName
                + ") query from MarkerInfo: " + e.getMessage(), e);
    }
    return 0;
}

From source file:org.generationcp.middleware.dao.gdms.MarkerInfoDAO.java

License:Open Source License

/**
 * Count the number of entries in marker info corresponding to the given genotype.
 *
 * @param genotype the genotype/*from ww  w  . j av  a2s.  com*/
 * @return the count
 * @throws MiddlewareQueryException
 */
public long countByGenotype(String genotype) throws MiddlewareQueryException {

    if (genotype == null) {
        return 0;
    }

    try {
        SQLQuery query = this.getSession().createSQLQuery(MarkerInfoDAO.COUNT_BY_GENOTYPE);
        query.setParameter("genotype", genotype);
        BigInteger result = (BigInteger) query.uniqueResult();
        if (result != null) {
            return result.longValue();
        }
        return 0;
    } catch (HibernateException e) {
        this.logAndThrowException("Error with countByGenotype(genotype=" + genotype
                + ") query from MarkerInfo: " + e.getMessage(), e);
    }
    return 0L;
}