List of usage examples for org.hibernate SQLQuery setTimeout
@Override
NativeQuery<T> setTimeout(int timeout);
From source file:com.mysema.query.jpa.hibernate.sql.AbstractHibernateSQLQuery.java
License:Apache License
private Query createQuery(String queryString) { logQuery(queryString);//from www . jav a2 s . co m org.hibernate.SQLQuery query = session.createSQLQuery(queryString); // set constants HibernateUtil.setConstants(query, constants, queryMixin.getMetadata().getParams()); // set entity paths for (Path<?> path : entityPaths) { query.addEntity(path.toString(), path.getType()); } // set result transformer, if projection is an EConstructor instance List<? extends Expression<?>> projection = queryMixin.getMetadata().getProjection(); if (projection.size() == 1 && projection.get(0) instanceof FactoryExpression) { query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection.get(0))); } if (fetchSize > 0) { query.setFetchSize(fetchSize); } if (timeout > 0) { query.setTimeout(timeout); } if (cacheable != null) { query.setCacheable(cacheable); } if (cacheRegion != null) { query.setCacheRegion(cacheRegion); } if (readOnly != null) { query.setReadOnly(readOnly); } return query; }
From source file:com.querydsl.jpa.hibernate.sql.AbstractHibernateSQLQuery.java
License:Apache License
private Query createQuery(boolean forCount) { NativeSQLSerializer serializer = (NativeSQLSerializer) serialize(forCount); String queryString = serializer.toString(); logQuery(queryString, serializer.getConstantToLabel()); org.hibernate.SQLQuery query = session.createSQLQuery(queryString); // set constants HibernateUtil.setConstants(query, serializer.getConstantToLabel(), queryMixin.getMetadata().getParams()); if (!forCount) { ListMultimap<Expression<?>, String> aliases = serializer.getAliases(); Set<String> used = Sets.newHashSet(); // set entity paths Expression<?> projection = queryMixin.getMetadata().getProjection(); if (projection instanceof FactoryExpression) { for (Expression<?> expr : ((FactoryExpression<?>) projection).getArgs()) { if (isEntityExpression(expr)) { query.addEntity(extractEntityExpression(expr).toString(), expr.getType()); } else if (aliases.containsKey(expr)) { for (String scalar : aliases.get(expr)) { if (!used.contains(scalar)) { query.addScalar(scalar); used.add(scalar); break; }// w w w . j a va2 s.c o m } } } } else if (isEntityExpression(projection)) { query.addEntity(extractEntityExpression(projection).toString(), projection.getType()); } else if (aliases.containsKey(projection)) { for (String scalar : aliases.get(projection)) { if (!used.contains(scalar)) { query.addScalar(scalar); used.add(scalar); break; } } } // set result transformer, if projection is a FactoryExpression instance if (projection instanceof FactoryExpression) { query.setResultTransformer(new FactoryExpressionTransformer((FactoryExpression<?>) projection)); } } if (fetchSize > 0) { query.setFetchSize(fetchSize); } if (timeout > 0) { query.setTimeout(timeout); } if (cacheable != null) { query.setCacheable(cacheable); } if (cacheRegion != null) { query.setCacheRegion(cacheRegion); } if (readOnly != null) { query.setReadOnly(readOnly); } return query; }
From source file:edu.vt.vbi.patric.dao.DBPRC.java
License:Apache License
public int getPRCCount(String taxonId, String filter) { String sql = "select count(*) cnt from (select distinct experiment_id, description, speciesname, processing_type, summary, pubmed_id, count(distinct sample_id) from app.post_genomic"; if (filter.equals("MS")) sql += " where processing_type = 'Mass spectrometry'"; else if (filter.equals("MA")) sql += " where processing_type = 'Microarray'"; else//from ww w . ja v a2s . c o m sql += " where processing_type = 'Protein interaction'"; sql += " and taxon_id in (" + DBSummary.getTaxonIdsInTaxonSQL(":taxonId") + ")"; sql += " group by experiment_id, description, speciesname, processing_type, summary, pubmed_id)"; Session session = factory.getCurrentSession(); session.beginTransaction(); SQLQuery q = session.createSQLQuery(sql).addScalar("cnt", Hibernate.INTEGER); q.setCacheable(true); q.setTimeout(SQL_TIMEOUT); q.setString("taxonId", taxonId); Object obj = q.uniqueResult(); session.getTransaction().commit(); return Integer.parseInt(obj.toString()); }
From source file:edu.vt.vbi.patric.dao.DBPRC.java
License:Apache License
public ArrayList<ResultType> getPRCData(String taxonId, String filter, int start, int end, String sort, String dir) {/*from w w w . jav a2s.com*/ String sql = "select distinct experiment_id, description, speciesname, processing_type, summary, pubmed_id, count(distinct sample_id) from app.post_genomic"; if (filter.equals("MS")) sql += " where processing_type = 'Mass spectrometry'"; else if (filter.equals("MA")) sql += " where processing_type = 'Microarray'"; else sql += " where processing_type = 'Protein interaction'"; sql += " and taxon_id in (" + DBSummary.getTaxonIdsInTaxonSQL(":taxonId") + ")"; sql += " group by experiment_id, description, speciesname, processing_type, summary, pubmed_id"; sql += " order by " + sort + " " + dir; Session session = factory.getCurrentSession(); session.beginTransaction(); SQLQuery q = session.createSQLQuery(sql); q.setTimeout(SQL_TIMEOUT); q.setString("taxonId", taxonId); ScrollableResults scr = null; try { scr = q.scroll(); } catch (Exception ex) { System.out.println("[SQL error]" + taxonId); ex.printStackTrace(); return null; } ArrayList<ResultType> results = new ArrayList<ResultType>(); Object[] obj = null; if (start > 1) { scr.setRowNumber(start - 1); } else { scr.beforeFirst(); } for (int i = start; (end > 0 && i < end && scr.next() == true) || (end == -1 && scr.next() == true); i++) { obj = scr.get(); ResultType row = new ResultType(); row.put("experiment_id", obj[0]); row.put("description", obj[1]); row.put("speciesname", obj[2]); row.put("experimenttype", obj[3]); row.put("summary", obj[4]); row.put("pubmed_id", obj[5]); row.put("samples", obj[6]); results.add(row); } session.getTransaction().commit(); return results; }