Refine search
Long longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.rowCount()).uniqueResult(); assertEquals(longValue, new Long(1)); longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.count("heightInches")).uniqueResult(); assertEquals(longValue, new Long(1)); Double dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.max( "heightInches" )).uniqueResult(); assertNotNull(dblValue); longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.max( "id" )).uniqueResult(); assertNotNull(longValue); dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "heightInches" )).uniqueResult(); assertNotNull(dblValue); dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "id" )).uniqueResult(); assertNotNull(dblValue); dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.avg( "bigIntegerValue" )).uniqueResult(); assertNotNull(dblValue); longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "id" )).uniqueResult(); assertNotNull(longValue); longValue = (Long) s.createCriteria( Human.class ).setProjection( Projections.sum( "intValue" )).uniqueResult(); assertNotNull(longValue); dblValue = (Double) s.createCriteria( Human.class ).setProjection( Projections.sum( "heightInches" )).uniqueResult(); assertNotNull(dblValue);
@Override public List<AssignedProductOptionDTO> findAssignedProductOptionsByProductId(Long productId) { Session session = em.unwrap(Session.class); Criteria criteria = session.createCriteria(SkuProductOptionValueXrefImpl.class); List dtoList = criteria .createAlias("sku", "sku") .createAlias("sku.product", "product") .createAlias("productOptionValue", "productOptionValue") .createAlias("productOptionValue.productOption", "productOption") .setProjection(Projections.distinct( Projections.projectionList() .add(Projections.property("product.id"), "productId") .add(Projections.property("productOption.attributeName"), "productOptionAttrName") .add(Projections.property("productOptionValue"), "productOptionValue") .add(Projections.property("sku"), "sku") ) ).setResultTransformer(Transformers.aliasToBean(AssignedProductOptionDTO.class)) .add(Restrictions.eq("product.id", productId)) .addOrder(Order.asc("productOption.attributeName")).list(); List<AssignedProductOptionDTO> results = new ArrayList<>(); for (Object o : dtoList) { AssignedProductOptionDTO dto = (AssignedProductOptionDTO) o; if (dto.getSku().isActive()) { results.add(dto); } } return results; }
Category category2 = new Category( 2, "Category2" ); Category category3 = new Category( 3, "Category3" ); session.persist( category1 ); session.persist( category2 ); session.persist( category3 ); session.flush(); session.persist( new Product2( 1, "Kit1", category1 ) ); List<Object[]> result = session.createCriteria( Category.class, "c" ).createAlias( "products", "p" ) .setProjection( Projections.projectionList() .add( Projections.groupProperty( "c.id" ) ) .add( Projections.countDistinct( "p.id" ) ) .addOrder( Order.asc( "c.id" ) ) .setFirstResult( 1 ).setMaxResults( 3 ).list();
protected Criteria getCriteria(Session s) { // should use RootEntityTransformer by default return s.createCriteria( Enrolment.class, "e" ) .createAlias( "e.student", "s", Criteria.LEFT_JOIN ) .setFetchMode( "student", FetchMode.JOIN ) .setFetchMode( "student.preferredCourse", FetchMode.JOIN ) .setProjection( Projections.projectionList() .add( Projections.property( "s.name" ) ) .add( Projections.property( "e.student" ) ) ) .addOrder( Order.asc( "s.studentNumber") ); } };
protected Criteria getCriteria(Session s) { return s.createCriteria( Enrolment.class ) .setProjection( Projections.projectionList() .add( Projections.min( "studentNumber" ).as( "minStudentNumber" ) ) .add( Projections.max( "studentNumber" ).as( "maxStudentNumber" ) ) ) .setResultTransformer( Transformers.ALIAS_TO_ENTITY_MAP ); } };
@Test public void testCreateWithNonEmptyOneToManyCollectionOfNew() { clearCounts(); Contract c = new Contract( null, "gail", "phone"); c.addParty( new Party( "party" ) ); Session s = openSession(); Transaction t = s.beginTransaction(); s.persist(c); t.commit(); s.close(); assertInsertCount( 2 ); assertUpdateCount( 0 ); clearCounts(); s = openSession(); t = s.beginTransaction(); c = (Contract) s.createCriteria( Contract.class ).uniqueResult(); assertEquals( 1, c.getParties().size() ); Party party = ( Party ) c.getParties().iterator().next(); assertEquals( "party", party.getName() ); if ( isContractPartiesBidirectional ) { assertSame( c, party.getContract() ); } s.delete(c); assertEquals( Long.valueOf( 0 ), s.createCriteria( Contract.class ).setProjection( Projections.rowCount() ).uniqueResult() ); assertEquals( Long.valueOf( 0 ), s.createCriteria( Party.class ).setProjection( Projections.rowCount() ).uniqueResult() ); t.commit(); s.close(); assertUpdateCount( 0 ); assertDeleteCount( 2 ); }
@Override public long getTotalUnmappedFindings() { Long maybeLong = (Long) sessionFactory.getCurrentSession().createCriteria(Finding.class) .add(Restrictions.eq("active", true)) .add(Restrictions.isNull("vulnerability")) .setProjection(Projections.rowCount()) .uniqueResult(); return maybeLong == null ? 0 : maybeLong; }
/** * @see org.openmrs.api.db.ConceptDAO#isConceptReferenceTermInUse(org.openmrs.ConceptReferenceTerm) */ @Override public boolean isConceptReferenceTermInUse(ConceptReferenceTerm term) throws DAOException { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptMap.class); criteria.add(Restrictions.eq("conceptReferenceTerm", term)); criteria.setProjection(Projections.rowCount()); if ((Long) criteria.uniqueResult() > 0) { return true; } criteria = sessionFactory.getCurrentSession().createCriteria(ConceptReferenceTermMap.class); criteria.add(Restrictions.eq("termB", term)); criteria.setProjection(Projections.rowCount()); return (Long) criteria.uniqueResult() > 0; }
public List<String> getNestedInteractionAcsByInteractionAc( String interactionAc ) { if ( log.isDebugEnabled() ) { log.debug( "Getting nested interactions for interaction with ac: " + interactionAc ); } return getSession().createCriteria( InteractionImpl.class ) .add( Restrictions.idEq( interactionAc ) ) .createAlias( "components", "comp" ) .createAlias( "comp.interactor", "interactor" ) .add( Restrictions.eq( "interactor.objClass", InteractionImpl.class.getName() ) ) .setProjection( Projections.distinct( Projections.property( "interactor.ac" ) ) ).list(); }
public static Map<String, Integer> countInteractionCountsForExperiments( String shortLabelLike ) { List<Object[]> expWithInteractionsCount = getSession().createCriteria( Experiment.class ) .add( Restrictions.like( "shortLabel", shortLabelLike ) ) .createAlias( "interactions", "int" ) .setProjection( Projections.projectionList() .add( Projections.distinct( Projections.property( "ac" ) ) ) .add( Projections.count( "int.ac" ) ) .add( Projections.groupProperty( "ac" ) ) ).list(); Map<String, Integer> interactionCountByExpAc = new HashMap<String, Integer>(); for ( Object[] expAndIntCount : expWithInteractionsCount ) { interactionCountByExpAc.put( (String) expAndIntCount[ 0 ], (Integer) expAndIntCount[ 1 ] ); } return interactionCountByExpAc; }
@Override public Long countPublicationsNotProcessedInProcess(Long corpusId, Long processId){ Session session = sessionFactory.getCurrentSession(); Criteria subQuery = session.createCriteria(CorpusHasPublicationsHasProcesses.class, "docsprocessed"); subQuery.createAlias("docsprocessed.corpusHasPublications", "allpubs", JoinType.RIGHT_OUTER_JOIN, Restrictions.eq("docsprocessed.id.chphpProcessesId", processId)); subQuery.add(Restrictions.eq("allpubs.id.chpCorpusId", corpusId)); subQuery.add(Restrictions.isNull("docsprocessed.id.chphpPublicationId")); subQuery.setProjection(Projections.count("allpubs.id.chpPublicationId")); Long count = (Long) subQuery.uniqueResult(); return count; }
@SuppressWarnings("unchecked") public List<ContentReviewItem> findByProviderGroupedBySiteAndTask(Integer providerId) { Criteria c = sessionFactory.getCurrentSession() .createCriteria(ContentReviewItem.class) .add(Restrictions.eq("providerId", providerId)) .setProjection( Projections.projectionList() .add(Projections.groupProperty("siteId")) .add(Projections.groupProperty("taskId"))); return c.list(); }
@Override @SuppressWarnings("unchecked") public List<Long> getProcessesIdsByResourceElements(Set<Long> resElemIds) { Session session = sessionFactory.getCurrentSession(); Criteria c = session.createCriteria(Annotations.class, "annnotations"); c.add(Restrictions.in("resourceElements.resId", resElemIds)); // c.add(Restrictions.eq("annActive", true)); //slowing the query... ProjectionList projections = Projections.projectionList(); projections.add(Projections.distinct(Projections.property("processes.proId")), "proId"); c.setProjection(projections); return c.list(); }
@Test public void testSaveOrUpdateManaged() { Session s = openSession(); Transaction tx = s.beginTransaction(); NumberedNode root = new NumberedNode( "root" ); s.saveOrUpdate( root ); tx.commit(); tx = s.beginTransaction(); NumberedNode child = new NumberedNode( "child" ); root.addChild( child ); s.saveOrUpdate( root ); assertFalse( s.contains( child ) ); s.flush(); assertTrue( s.contains( child ) ); tx.commit(); assertTrue( root.getChildren().contains( child ) ); assertEquals( root.getChildren().size(), 1 ); tx = s.beginTransaction(); assertEquals( Long.valueOf( 2 ), s.createCriteria( NumberedNode.class ) .setProjection( Projections.rowCount() ) .uniqueResult() ); s.delete( root ); s.delete( child ); tx.commit(); s.close(); }
protected Criteria getCriteria(Session s) throws Exception { return s.createCriteria( Student.class, "s" ) .setProjection( Projections.projectionList() .add( Projections.sqlProjection( "555 as studentNumber", new String[]{ "studentNumber" }, new Type[] { StandardBasicTypes.LONG } ) ) .add( Property.forName( "s.name" ).as( "name" ) ) ) .addOrder( Order.asc( "s.studentNumber" ) ) .setResultTransformer( new AliasToBeanConstructorResultTransformer( getConstructor() ) ); } private Constructor getConstructor() throws NoSuchMethodException {
public void testCountReturnValues() { Session s = openSession(); Transaction t = s.beginTransaction(); Human human1 = new Human(); human1.setName( new Name( "John", 'Q', "Public" ) ); human1.setNickName( "Johnny" ); s.save(human1); Human human2 = new Human(); human2.setName( new Name( "John", 'A', "Doe" ) ); human2.setNickName( "Johnny" ); s.save( human2 ); Human human3 = new Human(); human3.setName( new Name( "John", 'A', "Doe" ) ); .setProjection( Projections.rowCount() ) .uniqueResult(); .setProjection( Projections.count( "nickName" ) ) .setProjection( Projections.count( "nickName" ).setDistinct() ) .setProjection( Projections.count( "name" ).setDistinct() ) .setProjection( Projections.count( "name.first" ).setDistinct() ) .setProjection( Projections.count( "name" ) )
protected Criteria getCriteria(Session s) { return s.createCriteria( Enrolment.class, "e" ) .setProjection( Projections.property( "e.student" ) ) .addOrder( Order.asc( "e.studentNumber") ); } };
@SuppressWarnings("unchecked") public List<Concept> getConceptsByMapping(String code, String sourceName, boolean includeRetired) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ConceptMap.class); criteria.setProjection(Projections.property("concept")); criteria.createAlias("conceptReferenceTerm", "term"); criteria.add(Restrictions.eq("term.code", code).ignoreCase()); } else { criteria.add(Restrictions.eq("term.code", code)); criteria.createAlias("term.conceptSource", "source"); if (Context.getAdministrationService().isDatabaseStringComparisonCaseSensitive()) { criteria.add(Restrictions.or(Restrictions.eq("source.name", sourceName).ignoreCase(), Restrictions.eq( "source.hl7Code", sourceName).ignoreCase())); } else {
@Override public Object doInTransaction(TransactionStatus status) { PropertyProjection pipelineName = Projections.property("pipelineName"); Criteria criteria = sessionFactory.getCurrentSession().createCriteria(PipelineState.class).setProjection(pipelineName).add( Restrictions.eq("locked", true)); criteria.setCacheable(false); List<String> list = criteria.list(); return list; } });
public int countIVRCallSessionsCreatedBetweenDates(Date start, Date end) { return (Integer)sessionFactory .getCurrentSession() .createCriteria(IVRCallSession.class) .setProjection(Projections.rowCount()) .add(Restrictions.ge("created", start)) .add(Restrictions.le("created", end)) .list() .get(0); }