List of usage examples for org.hibernate.criterion Projections property
public static PropertyProjection property(String propertyName)
From source file:com.eucalyptus.autoscaling.common.internal.groups.PersistenceAutoScalingGroups.java
License:Open Source License
@Override public <T> List<T> listRequiringInstanceReplacement(final Function<? super AutoScalingGroup, T> transform) throws AutoScalingMetadataException { final DetachedCriteria criteria = DetachedCriteria.forClass(AutoScalingInstance.class) .add(Example.create(AutoScalingInstance.withHealthStatus(HealthStatus.Unhealthy))) .setProjection(Projections.property("autoScalingGroup")); return persistenceSupport.listByExample(AutoScalingGroup.withOwner(null), Predicates.alwaysTrue(), Property.forName("id").in(criteria), Collections.<String, String>emptyMap(), transform); }
From source file:com.eucalyptus.compute.common.internal.tags.FilterSupport.java
License:Open Source License
/** * Build a criterion that uses sub-selects to match the given tag restrictions *//*from w w w. j av a2 s. co m*/ private Criterion tagCriterion(final String accountId, final List<Junction> junctions) { final Junction conjunction = Restrictions.conjunction(); for (final Junction criterion : junctions) { final DetachedCriteria criteria = DetachedCriteria.forClass(tagClass) .add(Restrictions.eq("ownerAccountNumber", accountId)).add(criterion) .setProjection(Projections.property(resourceFieldName)); conjunction.add(Property.forName(tagFieldName).in(criteria)); } return conjunction; }
From source file:com.eucalyptus.compute.common.internal.vm.VmInstance.java
License:Open Source License
public static Projection instanceIdProjection() { return Projections.property("displayName"); }
From source file:com.eucalyptus.compute.common.internal.vm.VmInstance.java
License:Open Source License
public static Projection instanceUuidProjection() { return Projections.property("naturalId"); }
From source file:com.eucalyptus.entities.EntityCache.java
License:Open Source License
@SuppressWarnings("unchecked") private List<Pair<String, Integer>> loadVersionMap() { try (final TransactionResource db = Entities.transactionFor(example)) { List<Object[]> idVersionList = (List<Object[]>) Entities.createCriteria(example.getClass()) .add(Example.create(example)).setReadOnly(true).setCacheable(false).setFetchSize(1000) .setProjection(/*from ww w . ja va2s .c om*/ Projections.projectionList().add(Projections.id()).add(Projections.property("version"))) .list(); return Lists.newArrayList(Iterables.transform(idVersionList, ObjectArrayToStringIntPair.INSTANCE)); } }
From source file:com.evolveum.midpoint.repo.sql.query.custom.ShadowQueryWithDisjunction.java
License:Apache License
@Override public RQuery createQuery(ObjectQuery objectQuery, Class<? extends ObjectType> type, Collection<SelectorOptions<GetOperationOptions>> options, boolean countingObjects, Session session) { DetachedCriteria c1 = DetachedCriteria.forClass(ClassMapper.getHQLTypeClass(ShadowType.class), "s"); c1.createCriteria("strings", "s1", JoinType.LEFT_OUTER_JOIN); ParsedQuery parsedQuery = parse(objectQuery); Conjunction conjunction = Restrictions.conjunction(); conjunction/*from ww w .ja v a 2 s . com*/ .add(Restrictions.eq("resourceRef.targetOid", parsedQuery.refFilter.getValues().get(0).getOid())); Disjunction disjunction = Restrictions.disjunction(); disjunction.add(createAttributeEq(parsedQuery.eqUidFilter, parsedQuery.eqUidFilter.getPath().lastNamed().getName())); disjunction.add(createAttributeEq(parsedQuery.eqNameFilter, SchemaConstantsGenerated.ICF_S_NAME)); conjunction.add(disjunction); c1.add(conjunction); if (countingObjects) { c1.setProjection(Projections.countDistinct("s.oid")); return new RQueryCriteriaImpl(c1.getExecutableCriteria(session)); } c1.setProjection(Projections.distinct(Projections.property("s.oid"))); Criteria cMain = session.createCriteria(ClassMapper.getHQLTypeClass(ShadowType.class), "o"); cMain.add(Subqueries.propertyIn("oid", c1)); if (objectQuery != null && objectQuery.getPaging() != null) { cMain = updatePagingAndSorting(cMain, type, objectQuery.getPaging()); } ProjectionList projections = Projections.projectionList(); projections.add(Projections.property("fullObject")); projections.add(Projections.property("stringsCount")); projections.add(Projections.property("longsCount")); projections.add(Projections.property("datesCount")); projections.add(Projections.property("referencesCount")); projections.add(Projections.property("polysCount")); projections.add(Projections.property("booleansCount")); cMain.setProjection(projections); cMain.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER); return new RQueryCriteriaImpl(cMain); }
From source file:com.evolveum.midpoint.repo.sql.query.QueryInterpreter.java
License:Apache License
public Criteria interpret(ObjectQuery query, Class<? extends ObjectType> type, Collection<SelectorOptions<GetOperationOptions>> options, PrismContext prismContext, boolean countingObjects, Session session) throws QueryException { Validate.notNull(type, "Type must not be null."); Validate.notNull(session, "Session must not be null."); Validate.notNull(prismContext, "Prism context must not be null."); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Interpreting query for type '{}', query:\n{}", new Object[] { type, query }); }//ww w . j a v a2 s .co m Criteria criteria; if (query != null && query.getFilter() != null) { criteria = interpretQuery(query, type, prismContext, session); } else { criteria = session.createCriteria(ClassMapper.getHQLTypeClass(type)); } if (!countingObjects && query != null && query.getPaging() != null) { criteria = updatePagingAndSorting(criteria, type, query.getPaging()); } if (!countingObjects) { ProjectionList projections = Projections.projectionList(); projections.add(Projections.property("fullObject")); projections.add(Projections.property("stringsCount")); projections.add(Projections.property("longsCount")); projections.add(Projections.property("datesCount")); projections.add(Projections.property("referencesCount")); projections.add(Projections.property("polysCount")); criteria.setProjection(projections); } return criteria; }
From source file:com.evolveum.midpoint.repo.sql.query.restriction.OrgRestriction.java
License:Apache License
@Override public Criterion interpret(OrgFilter filter) throws QueryException { if (filter.isRoot()) { // Criteria pCriteria = getInterpreter().getCriteria(null); DetachedCriteria dc = DetachedCriteria.forClass(ROrgClosure.class); String[] strings = new String[1]; strings[0] = "descendant.oid"; Type[] type = new Type[1]; type[0] = StringType.INSTANCE;/*from w w w . ja va 2s .c o m*/ dc.setProjection(Projections.sqlGroupProjection("descendant_oid", "descendant_oid having count(descendant_oid)=1", strings, type)); // pCriteria.add(Subqueries.in("this.oid", dc)); return Subqueries.propertyIn("oid", dc); // Query rootOrgQuery = session.createQuery("select org from ROrg as org where org.oid in (select descendant.oid from ROrgClosure group by descendant.oid having count(descendant.oid)=1)"); } if (filter.getOrgRef() == null) { throw new QueryException("No organization reference defined in the search query."); } if (filter.getOrgRef().getOid() == null) { throw new QueryException( "No oid specified in organization reference " + filter.getOrgRef().debugDump()); } DetachedCriteria detached; switch (filter.getScope()) { case ONE_LEVEL: detached = DetachedCriteria.forClass(RParentOrgRef.class, "p"); detached.setProjection(Projections.distinct(Projections.property("p.ownerOid"))); detached.add(Restrictions.eq("p.targetOid", filter.getOrgRef().getOid())); break; case SUBTREE: default: detached = DetachedCriteria.forClass(ROrgClosure.class, "cl"); detached.setProjection(Projections.distinct(Projections.property("cl.descendantOid"))); detached.add(Restrictions.eq("cl.ancestorOid", filter.getOrgRef().getOid())); detached.add(Restrictions.ne("cl.descendantOid", filter.getOrgRef().getOid())); } String mainAlias = getContext().getAlias(null); return Subqueries.propertyIn(mainAlias + ".oid", detached); }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
private void addFullObjectProjectionList(String prefix, ProjectionList list, boolean group) { if (prefix == null) { prefix = ""; } else {//from ww w. j ava 2s.co m prefix = prefix + "."; } if (group) { list.add(Projections.groupProperty(prefix + "fullObject")); list.add(Projections.groupProperty(prefix + "stringsCount")); list.add(Projections.groupProperty(prefix + "longsCount")); list.add(Projections.groupProperty(prefix + "datesCount")); list.add(Projections.groupProperty(prefix + "referencesCount")); list.add(Projections.groupProperty(prefix + "polysCount")); } else { list.add(Projections.property(prefix + "fullObject")); list.add(Projections.property(prefix + "stringsCount")); list.add(Projections.property(prefix + "longsCount")); list.add(Projections.property(prefix + "datesCount")); list.add(Projections.property(prefix + "referencesCount")); list.add(Projections.property(prefix + "polysCount")); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void queryOrgTreeFindOrgs() throws Exception { Session session = open();// w w w. ja v a 2 s . c o m try { Criteria main = session.createCriteria(ROrg.class, "o"); ProjectionList projections = Projections.projectionList(); addFullObjectProjectionList("o", projections, false); main.setProjection(projections); DetachedCriteria detached = DetachedCriteria.forClass(RParentOrgRef.class, "p"); detached.setProjection(Projections.distinct(Projections.property("p.ownerOid"))); detached.add(Restrictions.eq("p.targetOid", "some oid")); main.add(Subqueries.propertyIn("o.oid", detached)); main.addOrder(Order.asc("o.name.orig")); String expected = HibernateToSqlTranslator.toSql(main); OrgFilter orgFilter = OrgFilter.createOrg("some oid", OrgFilter.Scope.ONE_LEVEL); ObjectQuery objectQuery = ObjectQuery.createObjectQuery(orgFilter); objectQuery .setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING)); String real = getInterpretedQuery(session, OrgType.class, objectQuery); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); OperationResult result = new OperationResult("query org structure"); repositoryService.searchObjects(OrgType.class, objectQuery, null, result); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }