List of usage examples for org.hibernate.criterion DetachedCriteria setProjection
public DetachedCriteria setProjection(Projection projection)
From source file:org.encuestame.persistence.dao.imp.TweetPollDao.java
License:Apache License
public final Long getTotalTweetPollResultByTweetPollSwitch(final TweetPollSwitch pollSwitch, final SearchPeriods period) { final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPollResult.class); criteria.setProjection(Projections.rowCount()); criteria.add(Restrictions.eq("tweetPollSwitch", pollSwitch)); calculateSearchPeriodsDates(period, criteria, "tweetResponseDate"); @SuppressWarnings("unchecked") List<TweetPollResult> results = (List<TweetPollResult>) getHibernateTemplate().findByCriteria(criteria); log.debug("Retrieve total tweetPolls by " + pollSwitch.getAnswers().getAnswer() + "--->" + results.size()); return (Long) (results.get(0) == null ? 0 : results.get(0)); }
From source file:org.encuestame.persistence.dao.imp.TweetPollDao.java
License:Apache License
@SuppressWarnings("unchecked") public List<Object[]> getTweetPollsRangeStats(final String tagName, final SearchPeriods period) { final DetachedCriteria detached = DetachedCriteria.forClass(TweetPoll.class) .createAlias("hashTags", "hashTags").setProjection(Projections.id()) .add(Subqueries.propertyIn("hashTags.hashTagId", DetachedCriteria.forClass(HashTag.class, "hash").setProjection(Projections.id()) .add(Restrictions.in("hash.hashTag", new String[] { tagName })))); final DetachedCriteria criteria = DetachedCriteria.forClass(TweetPoll.class, "tweetPoll"); criteria.add(Subqueries.propertyIn("tweetPoll.tweetPollId", detached)); criteria.addOrder(Order.desc("tweetPoll.createDate")); criteria.add(Restrictions.eq("publishTweetPoll", Boolean.TRUE)); ProjectionList projList = Projections.projectionList(); projList.add(Projections.groupProperty("createDate")); projList.add(Projections.rowCount()); criteria.setProjection(projList); calculateSearchPeriodsDates(period, criteria, "createDate"); return (List<Object[]>) getHibernateTemplate().findByCriteria(criteria); }
From source file:org.eulerframework.web.core.base.dao.impl.hibernate5.BaseDao.java
License:Apache License
@SuppressWarnings("unchecked") protected PageResponse<T> pageQuery(DetachedCriteria detachedCriteria, int pageIndex, int pageSize, Projection projection) {/*from www. jav a 2 s.co m*/ detachedCriteria.setProjection(Projections.rowCount()); long total = ((Long) detachedCriteria.getExecutableCriteria(this.getSessionFactory().getCurrentSession()) .list().get(0)).longValue(); detachedCriteria.setProjection(projection); if (projection != null) detachedCriteria.setResultTransformer(Transformers.aliasToBean(this.entityClass)); Criteria criteria = detachedCriteria.getExecutableCriteria(this.getSessionFactory().getCurrentSession()); criteria.setFirstResult((pageIndex - 1) * pageSize); criteria.setMaxResults(pageSize); List<T> result = criteria.list(); evict(result); return new PageResponse<>(result, total, pageIndex, pageSize); }
From source file:org.eurocarbdb.action.core.SearchGlycanSequence.java
License:Open Source License
public Criteria createCriteria() { DetachedCriteria crit = createSerializableCriteria(); crit.setProjection(Projections.distinct(Projections.property("glycanSequenceId"))); Criteria criteria = getEntityManager().createQuery(GlycanSequence.class); criteria.add(Subqueries.propertyIn("glycanSequenceId", crit)); if (getIndex() != null) { getIndex().apply(criteria);/*ww w . j a v a 2s . c o m*/ } return criteria; }
From source file:org.eurocarbdb.action.core.SearchGlycanSequence.java
License:Open Source License
private DetachedCriteria createSerializableCriteria() { // create base criteria log.debug("creating GlycanSequence criteria"); DetachedCriteria criteria;//w ww . ja v a2s . co m criteria = DetachedCriteria.forClass(GlycanSequence.class); // create biological contexts criteria DetachedCriteria bc_criteria = null; DetachedCriteria tax_criteria = null; DetachedCriteria tissue_criteria = null; DetachedCriteria disease_criteria = null; DetachedCriteria perturbation_criteria = null; if (taxonomyName != null || tissueName != null || diseaseName != null || perturbationName != null) { isNewQuery = true; log.debug("creating Biological context criteria"); bc_criteria = criteria.createCriteria("glycanContexts").createCriteria("biologicalContext", "bc"); // add taxonomy criteria if (taxonomyName != null) { log.debug("adding taxonomy query predicates for input string '" + taxonomyName + "'"); tax_criteria = bc_criteria.createCriteria("taxonomy", "taxa") .createCriteria("taxonomySupertypes", "supertax") .add(Restrictions.ilike("taxon", taxonomyName, MatchMode.EXACT)); } // add tissue criteria if (tissueName != null) { log.debug("adding tissue query predicates for input string '" + tissueName + "'"); tissue_criteria = bc_criteria.createCriteria("tissueTaxonomy", "ttax") .add(Restrictions.ilike("tissueTaxon", tissueName, MatchMode.EXACT)); } // add disease criteria if (diseaseName != null) { log.debug("adding disease query criteria for input string '" + diseaseName + "'"); disease_criteria = bc_criteria.createCriteria("diseaseContexts").createCriteria("disease", "dis") .add(Restrictions.ilike("diseaseName", diseaseName, MatchMode.EXACT)); } if (perturbationName != null) { log.debug("adding perturbation query criteria for input string '" + perturbationName + "'"); perturbation_criteria = bc_criteria.createCriteria("perturbationContexts") .createCriteria("perturbation", "per") .add(Restrictions.ilike("perturbationName", perturbationName, MatchMode.EXACT)); } } // add mass criteria boolean mass_query_is_given = false; boolean params_are_ok = false; if (exactMass > 0 && exactMassTolerance > 0) { isNewQuery = true; mass_query_is_given = true; lowMass = exactMass - exactMassTolerance; highMass = exactMass + exactMassTolerance; log.debug("adding predicates for exactMass=" + exactMass + " Da +/- " + exactMassTolerance + " Da (ie: " + lowMass + "-" + highMass + " Da)"); params_are_ok = true; } else if (lowMass > 0 && highMass > 0) { isNewQuery = true; mass_query_is_given = true; exactMass = -1; exactMassTolerance = -1; log.debug("adding predicates for mass range=(" + lowMass + ".." + highMass + " Da)"); params_are_ok = true; } if (mass_query_is_given) { if (params_are_ok) { isNewQuery = true; String property = useAvgMass ? "massAverage" : "massMonoisotopic"; criteria.add(Restrictions.between(property, new BigDecimal(lowMass), new BigDecimal(highMass))); } else { String msg = "Insufficient mass parameters given, either " + "provide an exactMass + exactMassTolerence + useAvgMass preference, " + "or provide a lowMass + highMass + useAvgMass preference"; addActionError(msg); log.info(msg); } } Glycan glycan = null; if (sequenceGWS != null) { glycan = Glycan.fromString(sequenceGWS); glycan.removeReducingEndModification(); if (glycan.isEmpty()) { glycan = null; sequenceGWS = null; } } if (glycan != null) { isNewQuery = true; // search structure in DB String glycoct = glycan.toGlycoCTCondensed(); SugarSequence seq = new SugarSequence(glycoct); SubstructureQuery query = new SubstructureQuery(seq); if (sequencePosition != null) { if (sequencePosition.equals("Core") || sequencePosition.equals("Core + Terminii")) query.setOption(Must_Include_Reducing_Terminus); if (sequencePosition.equals("Terminii") || sequencePosition.equals("Core + Terminii")) query.setOption(Must_Include_All_Non_Reducing_Terminii); } criteria.add(query.getQueryCriterion()); } if (this.additionalQueries.size() > 1) { isNewQuery = true; } for (SavedGlycanSequenceSearch oldQuery : this.additionalQueries) { DetachedCriteria oldCriteria = oldQuery.getQueryCriteria(); criteria.add(Subqueries.propertyIn("glycanSequenceId", oldCriteria)); oldCriteria.setProjection(Projections.distinct(Projections.property("glycanSequenceId"))); this.currentSearch = oldQuery; } return criteria; }
From source file:org.faster.orm.service.hibernate.HibernateCountService.java
License:Open Source License
@SuppressWarnings("rawtypes") private int doCount(DetachedCriteria criteria) { // ???/*from w w w. ja v a 2s. c om*/ DetachedCriteria clone = SerializationUtils.clone(criteria); clone.setProjection(Projections.rowCount()); Object value = null; try { value = fetchSingle(clone); } catch (Exception e) { // order???? Field criteriaImplFiled; // ?order try { criteriaImplFiled = DetachedCriteria.class.getDeclaredField("impl"); criteriaImplFiled.setAccessible(true); CriteriaImpl criteriaImpl = (CriteriaImpl) criteriaImplFiled.get(clone); Field orderField = CriteriaImpl.class.getDeclaredField("orderEntries"); orderField.setAccessible(true); List<?> orderEntries = (List<?>) orderField.get(criteriaImpl); // ?order? orderField.set(criteriaImpl, new ArrayList(0)); // order? try { value = fetchSingle(clone); } finally { orderField.set(criteriaImpl, orderEntries); // ??order? } } catch (Exception ee) { throw new RuntimeException("Can't count " + persistClassName + "![" + criteria + "]", ee); } } return value == null ? 0 : getIntValue(value); }
From source file:org.faster.orm.service.hibernate.with.option.HibernateProjectWithOptionService.java
License:Open Source License
@SuppressWarnings("unchecked") @Override// w ww . j av a 2 s .co m public List<ID> projectIdByCriteria(DetachedCriteria dc, QueryOption queryOption) { dc.setProjection(Projections.id()); return fetchAll(dc, queryOption); }
From source file:org.faster.orm.service.hibernate.with.property.HibernateGenericServiceWithName.java
License:Open Source License
@SuppressWarnings("unchecked") @Override/*from w w w.j a v a2 s . co m*/ public List<String> projectNameByCriteria(DetachedCriteria dc) { dc.setProjection(Projections.distinct(Projections.property(getFieldNameOfName()))); return fetchAll(dc); }
From source file:org.faster.orm.service.hibernate.with.property.HibernateGenericServiceWithNameAndTimestamp.java
License:Open Source License
@Override public Date findLastUpdateTime(DetachedCriteria criteria, QueryOption queryOption) { StopWatch sw = null;/*from w w w .ja v a 2 s . co m*/ if (log.isDebugEnabled()) { log.debug("Finding {} last update time with cache {} by {}", new Object[] { persistClassName, getCacheDisplay(queryOption.isCacheEnabled()), criteria }); sw = new StopWatch(); sw.start(); } criteria.setProjection(Projections.max(getFieldNameOfTimestamp())); Date ret = (Date) fetchSingle(criteria, queryOption); if (log.isDebugEnabled()) { log.debug("{} last update time is {}. ({} ms)", new Object[] { persistClassName, ret, sw.getTime() }); } return ret; }
From source file:org.gaixie.micrite.car.dao.hibernate.CarfileDAOImpl.java
License:Open Source License
@Override public int findCountByMaintainDateWillExpired(Integer carownerId, int fromDays, int toDays) { // TODO Auto-generated method stub DetachedCriteria criteria = DetachedCriteria.forClass(Carfile.class); if (carownerId != null) criteria.add(Expression.eq("carowner.id", carownerId)); criteria.add(Expression.eq("carStatus", ICarfileService.CARSTATUS_NORMAL)); criteria.add(Expression.eq("status", ICarfileService.STATUS_NORMAL)); criteria.add(Expression.between("daysToExpired", fromDays, toDays)); criteria.setProjection(Projections.rowCount()); return (Integer) getHibernateTemplate().findByCriteria(criteria).get(0); }