Example usage for org.hibernate ScrollableResults close

List of usage examples for org.hibernate ScrollableResults close

Introduction

In this page you can find the example usage for org.hibernate ScrollableResults close.

Prototype

void close();

Source Link

Document

Release resources immediately.

Usage

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }//from w w  w. ja v a  2 s  .  co m

    Query queryObject = session.createQuery(queryCondition.getQueryString());
    Object[] values = queryCondition.getObjArray();
    Type[] types = queryCondition.getTypeArray();
    if (null != values) {
        for (int i = 0; i < values.length; i++) {
            if (logger.isDebugEnabled()) {
                logger.debug("i=" + i);
                logger.debug("values[i]=" + values[i]);
                logger.debug("types[i]=" + types[i]);
            }
            if (types != null) {
                queryObject.setParameter(i, values[i], types[i]);
            } else {
                queryObject.setParameter(i, values[i]);
            }
        }
    }

    ScrollableResults sr = null;
    try {
        PageQueryResult queryResult = new PageQueryResult();
        queryResult.setQueryResult(new ArrayList());
        queryResult.setTotalCount(0);

        sr = queryObject.scroll();
        if (false == sr.last()) {

            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        int totalCount = sr.getRowNumber();
        queryResult.setTotalCount(totalCount + 1);

        int pageSize = queryCondition.getPageSize();
        int pageIndex = queryCondition.getPageIndex() - 1;

        int startRowNum = pageIndex * pageSize;
        if (false == sr.setRowNumber(startRowNum)) {
            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        List list = queryResult.getQueryResult();
        for (int i = 0; i < pageSize; i++) {
            list.add(sr.get());
            if (false == sr.next()) {
                break;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
        }
        return queryResult;
    } finally {
        if (null != sr)
            sr.close();
    }
}

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }/*from   ww  w . j  a v a 2  s.c  o m*/

    SQLQuery queryObject = session.createSQLQuery(queryCondition.getQueryString());
    Object[] values = queryCondition.getObjArray();
    Type[] types = queryCondition.getTypeArray();
    if (null != values) {
        for (int i = 0; i < values.length; i++) {
            if (logger.isDebugEnabled()) {
                logger.debug("i=" + i);
                logger.debug("values[i]=" + values[i]);
                logger.debug("types[i]=" + types[i]);
            }
            if (types != null) {
                queryObject.setParameter(i, values[i], types[i]);
            } else {
                queryObject.setParameter(i, values[i]);
            }
        }
    }

    ScrollableResults sr = null;
    try {
        PageQueryResult queryResult = new PageQueryResult();
        queryResult.setQueryResult(new ArrayList());
        queryResult.setTotalCount(0);

        sr = queryObject.scroll();
        if (false == sr.last()) {

            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        int totalCount = sr.getRowNumber();
        queryResult.setTotalCount(totalCount + 1);

        int pageSize = queryCondition.getPageSize();
        int pageIndex = queryCondition.getPageIndex() - 1;

        int startRowNum = pageIndex * pageSize;
        if (false == sr.setRowNumber(startRowNum)) {
            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        List list = queryResult.getQueryResult();
        for (int i = 0; i < pageSize; i++) {
            list.add(sr.get());
            if (false == sr.next()) {
                break;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
        }
        return queryResult;
    } finally {
        if (null != sr)
            sr.close();
    }
}

From source file:resource.dao.base.HQLDAO.java

public Object doInHibernate(Session session) throws HibernateException {
    if (logger.isDebugEnabled()) {
        logger.debug("doInHibernate(Session) - start"); //$NON-NLS-1$
    }/*  w  w w  .  ja v  a  2s . c  om*/

    Query queryObject = session.createQuery(queryCondition.getQueryString());
    Object[] values = queryCondition.getObjArray();
    Type[] types = queryCondition.getTypeArray();

    if (null != values) {
        for (int i = 0; i < values.length; i++) {
            if (logger.isDebugEnabled()) {
                logger.debug("i=" + i);
                logger.debug("values[i]=" + values[i]);
                logger.debug("types[i]=" + types[i]);
            }
            if (types != null) {
                queryObject.setParameter(i, values[i], types[i]);
            } else {
                queryObject.setParameter(i, values[i]);
            }
        }
    }

    ScrollableResults sr = null;
    try {
        RangeQueryResult queryResult = new RangeQueryResult();
        queryResult.setQueryResult(new ArrayList());
        queryResult.setTotalCount(0);

        sr = queryObject.scroll();
        if (false == sr.last()) {

            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        int totalCount = sr.getRowNumber();
        queryResult.setTotalCount(totalCount + 1);

        int startRowNum = queryCondition.getStart();
        if (false == sr.setRowNumber(startRowNum)) {
            if (logger.isDebugEnabled()) {
                logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
            }
            return queryResult;
        }

        List list = queryResult.getQueryResult();
        for (int i = 0; i < queryCondition.getCount(); i++) {
            list.add(sr.get());
            if (false == sr.next()) {
                break;
            }
        }

        if (logger.isDebugEnabled()) {
            logger.debug("doInHibernate(Session) - end"); //$NON-NLS-1$
        }
        return queryResult;
    } finally {
        if (null != sr)
            sr.close();
    }
}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * find category terms currently used in the database by evidence
 *//*  w w w.j  ava  2  s  . c  o  m*/
@Override
public Collection<CharacteristicValueObject> findEvidenceCategoryTerms() {

    Collection<CharacteristicValueObject> mgedCategory = new TreeSet<>();

    String queryString = "SELECT DISTINCT CATEGORY_URI, category FROM PHENOTYPE_ASSOCIATION "
            + "JOIN INVESTIGATION ON PHENOTYPE_ASSOCIATION.EXPERIMENT_FK = INVESTIGATION.ID "
            + "JOIN CHARACTERISTIC ON CHARACTERISTIC.INVESTIGATION_FK= INVESTIGATION.ID";
    org.hibernate.SQLQuery queryObject = this.getSessionFactory().getCurrentSession()
            .createSQLQuery(queryString);

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    while (results.next()) {

        CharacteristicValueObject characteristicValueObject = new CharacteristicValueObject(-1L);
        characteristicValueObject.setCategoryUri((String) results.get(0));
        characteristicValueObject.setCategory((String) results.get(1));
        mgedCategory.add(characteristicValueObject);
    }
    results.close();

    return mgedCategory;
}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

@Override
public Set<Long> findPrivateEvidenceId(Long taxonId, Integer limit) {

    String limitAbs;//  ww w  . jav  a 2 s .  co  m
    String orderBy;

    if (limit < 0) {
        limitAbs = "limit " + limit * -1;
        orderBy = "order by LAST_UPDATED asc ";
    } else {
        orderBy = "order by LAST_UPDATED desc ";
        limitAbs = "limit " + limit;
    }

    Set<Long> ids = new HashSet<>();

    String sqlQuery = "select distinct phen.ID ";
    sqlQuery += this.getPhenotypesGenesAssociationsBeginQuery(false);

    if (!SecurityUtil.isUserAdmin()) { // admins have no restrictions.
        if (!sqlQuery.trim().endsWith("where")) {
            sqlQuery += " AND ";
        }
        sqlQuery += EntityUtils.addGroupAndUserNameRestriction(true, false);
    }

    if (taxonId != null) {
        if (!sqlQuery.trim().endsWith("where")) {
            sqlQuery += " AND ";
        }
        sqlQuery += " tax.ID = :taxonId ";
    }

    sqlQuery += orderBy + limitAbs;

    SQLQuery queryObject = this.getSessionFactory().getCurrentSession().createSQLQuery(sqlQuery);

    if (taxonId != null) {
        queryObject.setParameter("taxonId", taxonId);
    }

    EntityUtils.addUserAndGroupParameters(queryObject, this.getSessionFactory());

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);

    while (results.next()) {
        Long phenotypeId = ((BigInteger) results.get(0)).longValue();
        ids.add(phenotypeId);
    }

    results.close();
    return ids;
}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * @param queryObject execute sqlQuery and populate phenotypesGenesAssociations is : phenotype --&gt; genes
 * @return map/*  w  w  w.  ja  v  a2 s.  c  o  m*/
 */
private Map<String, Set<Integer>> populateGenesAssociations(SQLQuery queryObject) {
    Map<String, Set<Integer>> phenotypesGenesAssociations = new HashMap<>();
    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    while (results.next()) {

        Integer geneNcbiId = (Integer) results.get(0);
        String valueUri = (String) results.get(1);

        EntityUtils.populateMapSet(phenotypesGenesAssociations, valueUri, geneNcbiId);
    }
    results.close();
    return phenotypesGenesAssociations;
}

From source file:ubic.gemma.persistence.service.association.phenotype.PhenotypeAssociationDaoImpl.java

License:Apache License

/**
 * @param queryObject execute sqlQuery and populate phenotypesGenesAssociations is : phenotype --&gt; genes
 * @return collection/*from  w ww.  j a  va  2 s . c  om*/
 */
private Collection<GeneEvidenceValueObject> populateGenesWithPhenotypes(SQLQuery queryObject) {
    StopWatch sw = new StopWatch();
    sw.start();

    // we accumulate the phenotypes for a gene in one VO
    Map<Long, GeneEvidenceValueObject> genesWithPhenotypes = new HashMap<>();

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    while (results.next()) {
        /* 0: gene id 1: ncbi id 2: name 3: symbol 4: taxon id 5: taxon name 6: characteristic value URI */
        Long geneId = ((BigInteger) results.get(0)).longValue();
        Integer nbciGeneId = (Integer) results.get(1);
        String officialName = (String) results.get(2);
        String officialSymbol = (String) results.get(3);
        Long taxonId = ((BigInteger) results.get(4)).longValue();
        String taxonCommonName = (String) results.get(5);
        String valueUri = (String) results.get(6);

        if (genesWithPhenotypes.get(geneId) != null) {
            genesWithPhenotypes.get(geneId).getPhenotypesValueUri().add(valueUri);
        } else {
            GeneEvidenceValueObject g = new GeneEvidenceValueObject(geneId);
            g.setNcbiId(nbciGeneId);
            g.setOfficialName(officialName);
            g.setOfficialSymbol(officialSymbol);
            g.setTaxonCommonName(taxonCommonName);
            g.setTaxonId(taxonId);
            g.getPhenotypesValueUri().add(valueUri);
            genesWithPhenotypes.put(geneId, g);
        }
    }
    results.close();

    if (sw.getTime() > 500) {
        PhenotypeAssociationDaoImpl.log
                .info("Get " + genesWithPhenotypes.size() + " genes with phenotypes: " + sw.getTime() + "ms");
    }

    return genesWithPhenotypes.values();
}

From source file:ubic.gemma.persistence.service.expression.bioAssayData.DesignElementDataVectorDaoImpl.java

License:Apache License

private void getVectorsBatch(Map<Long, Collection<Long>> cs2gene, org.hibernate.Query queryObject,
        Map<T, Collection<Long>> dedv2genes, Collection<Long> batch) {
    queryObject.setParameterList("cs", batch);
    queryObject.setFlushMode(FlushMode.MANUAL);
    queryObject.setReadOnly(true);/*from  www .  ja va  2s. c o  m*/
    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);

    while (results.next()) {
        @SuppressWarnings("unchecked")
        T dedv = (T) results.get(0);
        Long cs = (Long) results.get(1);
        Collection<Long> associatedGenes = cs2gene.get(cs);
        if (!dedv2genes.containsKey(dedv)) {
            dedv2genes.put(dedv, associatedGenes);
        } else {
            Collection<Long> mappedGenes = dedv2genes.get(dedv);
            mappedGenes.addAll(associatedGenes);
        }
    }

    results.close();
}

From source file:ubic.gemma.persistence.util.CommonQueries.java

License:Apache License

/**
 * @param session      session//from w  ww.j  a v a  2  s.  c  om
 * @param genes        genes
 * @param arrayDesigns array design
 * @return map of probe IDs to collections of gene IDs.
 */
public static Map<Long, Collection<Long>> getCs2GeneIdMap(Collection<Long> genes, Collection<Long> arrayDesigns,
        Session session) {

    Map<Long, Collection<Long>> cs2genes = new HashMap<>();

    String queryString = "SELECT CS AS csid, GENE AS geneId FROM GENE2CS g WHERE g.GENE IN (:geneIds) AND g.AD IN (:ads)";
    SQLQuery queryObject = session.createSQLQuery(queryString);
    queryObject.addScalar("csid", LongType.INSTANCE);
    queryObject.addScalar("geneId", LongType.INSTANCE);
    queryObject.setParameterList("ads", arrayDesigns);
    queryObject.setParameterList("geneIds", genes);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    CommonQueries.addGeneIds(cs2genes, results);
    results.close();

    return cs2genes;

}

From source file:ubic.gemma.persistence.util.CommonQueries.java

License:Apache License

public static Map<CompositeSequence, Collection<Gene>> getCs2GeneMap(Collection<Gene> genes,
        Collection<ArrayDesign> arrayDesigns, Session session) {

    StopWatch timer = new StopWatch();
    timer.start();//from www.j av  a 2  s . c  o m
    final String csQueryString = "select distinct cs, gene from Gene as gene"
            + " inner join gene.products gp, BioSequence2GeneProduct ba, CompositeSequence cs "
            + " where ba.bioSequence=cs.biologicalCharacteristic and ba.geneProduct = gp"
            + " and gene in (:genes) and cs.arrayDesign in (:ads) ";

    Map<CompositeSequence, Collection<Gene>> cs2gene = new HashMap<>();
    Query queryObject = session.createQuery(csQueryString);
    queryObject.setCacheable(true);
    queryObject.setParameterList("genes", genes);
    queryObject.setParameterList("ads", arrayDesigns);
    queryObject.setReadOnly(true);
    queryObject.setFlushMode(FlushMode.MANUAL);

    ScrollableResults results = queryObject.scroll(ScrollMode.FORWARD_ONLY);
    CommonQueries.addGenes(cs2gene, results);
    results.close();
    if (timer.getTime() > 200) {
        CommonQueries.log.info("Get cs2gene for " + genes.size() + " :" + timer.getTime() + "ms");
    }
    return cs2gene;

}