List of usage examples for org.hibernate.criterion DetachedCriteria setProjection
public DetachedCriteria setProjection(Projection projection)
From source file:alpha.portal.dao.hibernate.AlphaCardDaoHibernate.java
License:Apache License
/** * List alpha cards by criterion.//from ww w . java2 s.c o m * * @param caseId * the case id * @param criteriaArray * the criteria array * @return the list * @see alpha.portal.dao.AlphaCardDao#listAlphaCardsByCriterion(org.hibernate.criterion.Criterion) */ public List<AlphaCard> listAlphaCardsByCriterion(final String caseId, final Criterion... criteriaArray) { Session session; boolean sessionOwn = false; try { session = this.getSessionFactory().getCurrentSession(); } catch (final Exception e) { session = this.getSessionFactory().openSession(); sessionOwn = true; } // get newest sequenceNumber for each cardId final DetachedCriteria version = DetachedCriteria.forClass(AlphaCard.class, "cardVersion") .add(Property.forName("card.alphaCardIdentifier.cardId") .eqProperty("cardVersion.alphaCardIdentifier.cardId")) .setProjection( Projections.projectionList().add(Projections.max("alphaCardIdentifier.sequenceNumber"))); final Criteria crit = session.createCriteria(AlphaCard.class, "card"); for (final Criterion c : criteriaArray) { // Create the subquery (somehow we need to use the Descriptor or // else the subquery-JOIN is not done) final DetachedCriteria subcrit = DetachedCriteria.forClass(AlphaCardDescriptor.class, "crit"); // Join the adornments subcrit.createAlias("crit.adornmentList", "ad"); // Add adornment condition subcrit.add(c); // Map the subquery back to the outer query subcrit.add(Restrictions.eqProperty("card.alphaCardIdentifier", "crit.alphaCardIdentifier")); // Narrow down subquery or else we get a NullPointer-Exception subcrit.setProjection(Projections.property("crit.alphaCardIdentifier.cardId")); // Add this subquery to the outer query. crit.add(Subqueries.exists(subcrit)); } crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY) .add(Property.forName("alphaCardIdentifier.sequenceNumber").eq(version)) .createAlias("alphaCase", "case").add(Restrictions.eq("case.caseId", caseId)); List<AlphaCard> list = crit.list(); if (list.size() > 1) { final List<AlphaCard> order = (list.get(0)).getAlphaCase().getAlphaCards(); final List<AlphaCard> orderedList = new LinkedList<AlphaCard>(); for (final AlphaCard cc : order) { for (final AlphaCard c : list) { if (c.getAlphaCardIdentifier().equals(cc.getAlphaCardIdentifier())) { orderedList.add(c); break; } } } list = orderedList; } if (sessionOwn) { session.close(); } return list; }
From source file:ar.com.zauber.commons.repository.SpringHibernateRepository.java
License:Apache License
/** @see Repository#find(Query) */ @SuppressWarnings("unchecked") public final <T extends Persistible> List<T> find(final Query<T> query) { CriteriaSpecification criteria = getCriteriaSpecification(null, query, false); final SimpleQuery<T> simpleQuery = (SimpleQuery<T>) query; Criteria aCriteria;/* ww w . j av a 2s .co m*/ // TODO Esto debera ir en el metodo que hace getCriteriaSpecification // pero como no tiene DetachedCriteria posibilidad de setearle valores // para paginacin hubo que hacerlo as. if (simpleQuery.getPaging() != null) { int firstResult = (simpleQuery.getPaging().getPageNumber() - 1) * simpleQuery.getPaging().getResultsPerPage(); DetachedCriteria idsDetachedCriteria = (DetachedCriteria) criteria; idsDetachedCriteria.setProjection(Projections.id()); Criteria idsCriteria = idsDetachedCriteria.getExecutableCriteria(this.getSession()); idsCriteria.setCacheable(query.getCacheable()); idsCriteria.setFirstResult(firstResult); idsCriteria.setMaxResults(simpleQuery.getPaging().getResultsPerPage()); // Se hace primero el select de los IDs y luego el de los objetos en // si ya que de otra manera en aquellos casos que haya objetos // que tienen colecciones cuenta los mismos varias veces haciendo // que se devuelvan menos resultados. List<Long> ids = idsCriteria.list(); DetachedCriteria theCriteria = (DetachedCriteria) getCriteriaSpecification(null, query, false); if (ids.isEmpty()) { return new ArrayList<T>(); } theCriteria.add(Restrictions.in("id", ids)); aCriteria = theCriteria.getExecutableCriteria(this.getSession()); } else { aCriteria = ((DetachedCriteria) criteria).getExecutableCriteria(this.getSession()); } aCriteria.setCacheable(query.getCacheable()); return aCriteria.list(); }
From source file:ar.com.zauber.commons.repository.SpringHibernateRepository.java
License:Apache License
/** @see Repository#aggregate(Query, Class) */ @SuppressWarnings("unchecked") public final <R, T extends Persistible> R aggregate(final Query<T> query, final AggregateFunction aggregateFunction, final Class<R> retClazz) { Validate.notNull(query);/*from w w w . j a va 2 s.co m*/ Validate.notNull(aggregateFunction); Validate.notNull(retClazz); boolean ignoreOrder = true; if (aggregateFunction instanceof CompositeAggregateFunction) { List<AggregateFunction> list = ((CompositeAggregateFunction) aggregateFunction).getFunctions(); for (AggregateFunction a : list) { if (a instanceof GroupPropertyAggregateFilter) { ignoreOrder = false; } } } else if (aggregateFunction instanceof GroupPropertyAggregateFilter) { ignoreOrder = false; } final DetachedCriteria criteria = (DetachedCriteria) getCriteriaSpecification(null, query, ignoreOrder); final ProjectionAggregateFunctionVisitor visitor = new ProjectionAggregateFunctionVisitor(); aggregateFunction.accept(visitor); criteria.setProjection(visitor.getProjection()); return (R) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(final Session session) throws HibernateException, SQLException { criteria.getExecutableCriteria(session).setCacheable(query.getCacheable()); final List l = criteria.getExecutableCriteria(session).list(); return l.size() == 0 ? null : l.size() == 1 ? l.get(0) : l; } }); }
From source file:au.org.theark.lims.model.dao.BioCollectionDao.java
License:Open Source License
public Boolean hasBioCollections(LinkSubjectStudy linkSubjectStudy) { // Use WHERE EXIST to optimise query even further StatelessSession session = getStatelessSession(); Criteria criteria = session.createCriteria(LinkSubjectStudy.class, "lss"); DetachedCriteria sizeCriteria = DetachedCriteria.forClass(BioCollection.class, "bc"); criteria.add(Restrictions.eq("lss.id", linkSubjectStudy.getId())); sizeCriteria.add(Property.forName("lss.id").eqProperty("bc.linkSubjectStudy.id")); criteria.add(Subqueries.exists(sizeCriteria.setProjection(Projections.property("bc.id")))); criteria.setProjection(Projections.rowCount()); Boolean result = ((Long) criteria.uniqueResult()) > 0L; session.close();/*from ww w . jav a 2 s . c o m*/ return result; }
From source file:au.org.theark.lims.model.dao.BioCollectionDao.java
License:Open Source License
public Boolean hasBiospecimens(BioCollection bioCollection) { // Use WHERE EXIST to optimise query even further StatelessSession session = getStatelessSession(); Criteria criteria = session.createCriteria(BioCollection.class, "bc"); DetachedCriteria sizeCriteria = DetachedCriteria.forClass(Biospecimen.class, "b"); criteria.add(Restrictions.eq("bc.id", bioCollection.getId())); sizeCriteria.add(Property.forName("bc.id").eqProperty("b.bioCollection.id")); criteria.add(Subqueries.exists(sizeCriteria.setProjection(Projections.property("b.id")))); criteria.setProjection(Projections.rowCount()); Boolean result = ((Long) criteria.uniqueResult()) > 0L; session.close();/*from w w w. j a v a2 s . c o m*/ return result; }
From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java
License:Open Source License
/** * The method checks if the given questionnaire's fields have data linked to it. * //from ww w . j a v a 2 s . c om * @param customFieldGroup */ public void isDataAvailableForQuestionnaire(CustomFieldGroup customFieldGroup) { Criteria criteria = getSession().createCriteria(CustomField.class, "cf"); criteria.createAlias("customFieldDisplay", "cfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay criteria.createAlias("cfd.customFieldGroup", "cfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup criteria.add(Restrictions.eq("cf.study", customFieldGroup.getStudy())); ArkFunction function = iArkCommonService .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_DATA_DICTIONARY); criteria.add(Restrictions.eq("cf.arkFunction", function)); criteria.add(Restrictions.eq("cfg.id", customFieldGroup.getId())); DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd"); // Join CustomFieldDisplay and PhenoData on ID FK fieldDataCriteria.add(Property.forName("cfd.id").eqProperty("pd." + "customFieldDisplay.id")); criteria.add( Subqueries.exists(fieldDataCriteria.setProjection(Projections.property("pd.customFieldDisplay")))); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("cfg.name"), "questionnaire"); projectionList.add(Projections.property("cf.name"), "fieldName"); projectionList.add(Projections.property("cf.description"), "description"); }
From source file:au.org.theark.report.model.dao.ReportDao.java
License:Open Source License
public List<ConsentDetailsDataRow> getStudyCompConsentList(ConsentDetailsReportVO cdrVO) { // NB: There should only ever be one Consent record for a particular Subject for a particular StudyComp List<ConsentDetailsDataRow> results = new ArrayList<ConsentDetailsDataRow>(); Criteria criteria = getSession().createCriteria(LinkSubjectStudy.class, "lss"); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("lss." + "subjectUID"), "subjectUID"); criteria.add(/* w ww . j av a2 s .c o m*/ Restrictions.eq("lss." + Constants.LINKSUBJECTSTUDY_STUDY, cdrVO.getLinkSubjectStudy().getStudy())); if (cdrVO.getLinkSubjectStudy().getSubjectUID() != null) { criteria.add(Restrictions.ilike("lss." + Constants.LINKSUBJECTSTUDY_SUBJECTUID, cdrVO.getLinkSubjectStudy().getSubjectUID(), MatchMode.ANYWHERE)); } if (cdrVO.getLinkSubjectStudy().getSubjectStatus() != null) { criteria.add(Restrictions.eq("lss." + Constants.LINKSUBJECTSTUDY_SUBJECTSTATUS, cdrVO.getLinkSubjectStudy().getSubjectStatus())); } if (cdrVO.getConsentDate() != null) { // NB: constraint on consentDate or consentStatus automatically removes "Not Consented" state // So LinkSubjectStudy inner join to Consent ok for populated consentDate criteria.createAlias("lss." + Constants.LINKSUBJECTSTUDY_CONSENT, "c"); criteria.createAlias("c." + Constants.CONSENT_CONSENTSTATUS, "cs"); // constrain on studyComp criteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp())); // constrain on consentDate criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTDATE, cdrVO.getConsentDate())); // ConsentStatus is optional for this query... if (cdrVO.getConsentStatus() != null) { criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus())); } projectionList.add(Projections.property("cs.name"), "consentStatus"); projectionList.add(Projections.property("c." + Constants.CONSENT_CONSENTDATE), "consentDate"); } else if (cdrVO.getConsentStatus() != null) { if (cdrVO.getConsentStatus().getName().equals(Constants.NOT_CONSENTED)) { // Need to handle "Not Consented" status differently (since it doesn't have a Consent record) // Helpful website: http://www.cereslogic.com/pages/2008/09/22/hibernate-criteria-subqueries-exists/ // Build subquery to find all Consent records for a Study Comp DetachedCriteria consentCriteria = DetachedCriteria.forClass(Consent.class, "c"); // Constrain on StudyComponent consentCriteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp())); // Just in case "Not Consented" is erroneously entered into a row in the Consent table // consentCriteria.add(Restrictions.ne("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus())); // Join LinkSubjectStudy and Consent on ID FK consentCriteria.add(Property.forName("c.linkSubjectStudy.id").eqProperty("lss." + "id")); criteria.add(Subqueries.notExists(consentCriteria.setProjection(Projections.property("c.id")))); // If Consent records follows design for "Not Consented", then: // - consentStatus and consentDate are not populated } else { // NB: constraint on consentDate or consentStatus automatically removes "Not Consented" state // So LinkSubjectStudy inner join to Consent ok for all recordable consentStatus criteria.createAlias("lss." + Constants.LINKSUBJECTSTUDY_CONSENT, "c"); criteria.createAlias("c." + Constants.CONSENT_CONSENTSTATUS, "cs"); // Constrain on StudyComponent criteria.add(Restrictions.eq("c." + Constants.CONSENT_STUDYCOMP, cdrVO.getStudyComp())); // ConsentStatus is NOT optional for this query! criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTSTATUS, cdrVO.getConsentStatus())); if (cdrVO.getConsentDate() != null) { criteria.add(Restrictions.eq("c." + Constants.CONSENT_CONSENTDATE, cdrVO.getConsentDate())); } projectionList.add(Projections.property("cs.name"), "consentStatus"); projectionList.add(Projections.property("c." + Constants.CONSENT_CONSENTDATE), "consentDate"); } } else { // Should not attempt to run this query with no consentDate nor consentStatus criteria provided log.error( "reportDao.getStudyCompConsentList(..) is missing consentDate or consentStatus parameters in the VO"); return null; } criteria.addOrder(Order.asc("lss." + "subjectUID")); criteria.setProjection(projectionList); criteria.setResultTransformer(Transformers.aliasToBean(ConsentDetailsDataRow.class)); // This gives a list of subjects matching the specific studyComp and consentStatus results = criteria.list(); return results; }
From source file:au.org.theark.report.model.dao.ReportDao.java
License:Open Source License
public List<FieldDetailsDataRow> getPhenoFieldDetailsList(FieldDetailsReportVO fdrVO) { List<FieldDetailsDataRow> results = new ArrayList<FieldDetailsDataRow>(); Criteria criteria = getSession().createCriteria(PhenoDataSetCollection.class, "fpc"); criteria.createAlias("phenoCollection", "pc"); // Inner join to Field criteria.createAlias("field", "f"); // Inner join to Field criteria.createAlias("f.fieldType", "ft"); // Inner join to FieldType criteria.add(Restrictions.eq("study", fdrVO.getStudy())); if (fdrVO.getPhenoCollection() != null) { criteria.add(Restrictions.eq("phenoCollection", fdrVO.getPhenoCollection())); }/* w w w . j a v a2 s. c o m*/ if (fdrVO.getFieldDataAvailable()) { DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "fd"); // Join FieldPhenoCollection and FieldData on ID FK fieldDataCriteria.add(Property.forName("f.id").eqProperty("fd." + "field.id")); fieldDataCriteria.add(Property.forName("pc.id").eqProperty("fd." + "collection.id")); criteria.add(Subqueries.exists(fieldDataCriteria.setProjection(Projections.property("fd.id")))); } ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("pc.name"), "collection"); projectionList.add(Projections.property("f.name"), "fieldName"); projectionList.add(Projections.property("f.description"), "description"); projectionList.add(Projections.property("f.minValue"), "minValue"); projectionList.add(Projections.property("f.maxValue"), "maxValue"); projectionList.add(Projections.property("f.encodedValues"), "encodedValues"); projectionList.add(Projections.property("f.missingValue"), "missingValue"); projectionList.add(Projections.property("f.units"), "units"); projectionList.add(Projections.property("ft.name"), "type"); criteria.setProjection(projectionList); // only return fields required for report criteria.setResultTransformer(Transformers.aliasToBean(FieldDetailsDataRow.class)); criteria.addOrder(Order.asc("pc.id")); criteria.addOrder(Order.asc("f.name")); results = criteria.list(); return results; }
From source file:au.org.theark.report.model.dao.ReportDao.java
License:Open Source License
public List<CustomFieldDetailsDataRow> getPhenoCustomFieldDetailsList(CustomFieldDetailsReportVO fdrVO) { List<CustomFieldDetailsDataRow> results = new ArrayList<CustomFieldDetailsDataRow>(); if (fdrVO.getCustomFieldDisplay() != null) { /*//from w w w .j ava 2s . c om * Following query returns customFields whether or not they are * associated with a customFieldGroups (via customFieldDisplay) */ Criteria criteria = getSession().createCriteria(CustomField.class, "cf"); criteria.createAlias("customFieldDisplay", "cfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay criteria.createAlias("cfd.customFieldGroup", "cfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup criteria.createAlias("fieldType", "ft", JoinType.LEFT_OUTER_JOIN); // Left join to FieldType criteria.createAlias("unitType", "ut", JoinType.LEFT_OUTER_JOIN); // Left join to UnitType criteria.add(Restrictions.eq("cf.study", fdrVO.getStudy())); ArkFunction function = iArkCommonService .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION); criteria.add(Restrictions.eq("cf.arkFunction", function)); if (fdrVO.getCustomFieldDisplay().getCustomFieldGroup() != null) { criteria.add( Restrictions.eq("cfg.id", fdrVO.getCustomFieldDisplay().getCustomFieldGroup().getId())); } if (fdrVO.getFieldDataAvailable()) { DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd"); // Join CustomFieldDisplay and PhenoData on ID FK fieldDataCriteria.add(Property.forName("cfd.id").eqProperty("pd." + "customFieldDisplay.id")); criteria.add(Subqueries .exists(fieldDataCriteria.setProjection(Projections.property("pd.customFieldDisplay")))); } ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("cfg.name"), "questionnaire"); projectionList.add(Projections.property("cf.name"), "fieldName"); projectionList.add(Projections.property("cf.description"), "description"); projectionList.add(Projections.property("cf.minValue"), "minValue"); projectionList.add(Projections.property("cf.maxValue"), "maxValue"); projectionList.add(Projections.property("cf.encodedValues"), "encodedValues"); projectionList.add(Projections.property("cf.missingValue"), "missingValue"); projectionList.add(Projections.property("ut.name"), "units"); projectionList.add(Projections.property("ft.name"), "type"); criteria.setProjection(projectionList); // only return fields required for report criteria.setResultTransformer(Transformers.aliasToBean(CustomFieldDetailsDataRow.class)); criteria.addOrder(Order.asc("cfg.id")); criteria.addOrder(Order.asc("cfd.sequence")); results = criteria.list(); } return results; }
From source file:au.org.theark.report.model.dao.ReportDao.java
License:Open Source License
public List<PhenoDataSetFieldDetailsDataRow> getPhenoDataSetFieldDetailsList( PhenoDataSetFieldDetailsReportVO reportVO) { List<PhenoDataSetFieldDetailsDataRow> results = new ArrayList<PhenoDataSetFieldDetailsDataRow>(); if (reportVO.getPhenoDataSetFieldDisplay() != null) { /*//w w w . j a v a2 s. co m * Following query returns customFields whether or not they are * associated with a customFieldGroups (via customFieldDisplay) */ Criteria criteria = getSession().createCriteria(PhenoDataSetField.class, "pf"); criteria.createAlias("phenoDatasetFieldDisplay", "pdfd", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldDisplay criteria.createAlias("pdfd.phenoDatasetFieldGroup", "pdfg", JoinType.LEFT_OUTER_JOIN); // Left join to CustomFieldGroup criteria.createAlias("fieldType", "ft", JoinType.LEFT_OUTER_JOIN); // Left join to FieldType criteria.createAlias("unitType", "ut", JoinType.LEFT_OUTER_JOIN); // Left join to UnitType criteria.add(Restrictions.eq("pf.study", reportVO.getStudy())); ArkFunction function = iArkCommonService .getArkFunctionByName(au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION); criteria.add(Restrictions.eq("pf.arkFunction", function)); if (reportVO.getPhenoDataSetFieldDisplay().getPhenoDataSetGroup() != null) { criteria.add(Restrictions.eq("pdfg.id", reportVO.getPhenoDataSetFieldDisplay().getPhenoDataSetGroup().getId())); } if (reportVO.getFieldDataAvailable()) { DetachedCriteria fieldDataCriteria = DetachedCriteria.forClass(PhenoDataSetData.class, "pd"); // Join CustomFieldDisplay and PhenoData on ID FK fieldDataCriteria .add(Property.forName("pdfd.id").eqProperty("pd." + "phenoDatasetFieldDisplay.id")); criteria.add(Subqueries.exists( fieldDataCriteria.setProjection(Projections.property("pd.phenoDatasetFieldDisplay")))); } ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("pdfg.name"), "questionnaire"); projectionList.add(Projections.property("pf.name"), "fieldName"); projectionList.add(Projections.property("pf.description"), "description"); projectionList.add(Projections.property("pf.minValue"), "minValue"); projectionList.add(Projections.property("pf.maxValue"), "maxValue"); projectionList.add(Projections.property("pf.encodedValues"), "encodedValues"); projectionList.add(Projections.property("pf.missingValue"), "missingValue"); projectionList.add(Projections.property("ut.name"), "units"); projectionList.add(Projections.property("ft.name"), "type"); criteria.setProjection(projectionList); // only return fields required for report criteria.setResultTransformer(Transformers.aliasToBean(PhenoDataSetFieldDetailsDataRow.class)); criteria.addOrder(Order.asc("pdfg.id")); criteria.addOrder(Order.asc("pdfd.sequence")); results = criteria.list(); } return results; }