List of usage examples for org.hibernate.criterion DetachedCriteria setProjection
public DetachedCriteria setProjection(Projection projection)
From source file:com.ephesoft.dcma.da.dao.hibernate.SecurityUserDaoImpl.java
License:Open Source License
@Override public Set<String> getAllUser() { LOGGER.debug("Fetching all users from Database."); Set<String> userNameSet = null; final DetachedCriteria criteria = criteria(); if (null != criteria) { criteria.setProjection(Projections.property(USER_NAME)); final List<String> userNameList = this.find(criteria); if (null != userNameList && !userNameList.isEmpty()) { userNameSet = new HashSet<String>(userNameList); }/*from ww w .j a v a2 s. co m*/ } LOGGER.debug("User Set is: ", userNameSet); return userNameSet; }
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 w w w. ja v a 2s . c o m*/ .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.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;// w w w. j a va2s . 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
@Test public void queryOrgTreeFindOrgs() throws Exception { Session session = open();/* w w w .j a 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); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void test310QueryNameAndOrg() throws Exception { Session session = open();/*ww w . j a v a 2 s .c o m*/ try { DetachedCriteria detached = DetachedCriteria.forClass(ROrgClosure.class, "cl"); detached.setProjection(Projections.distinct(Projections.property("cl.descendantOid"))); detached.add(Restrictions.eq("cl.ancestorOid", "1234")); detached.add(Restrictions.ne("cl.descendantOid", "1234")); Criteria main = session.createCriteria(RUser.class, "u"); String mainAlias = "u"; 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")); main.setProjection(projections); Conjunction c = Restrictions.conjunction(); c.add(Restrictions.and(Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"), Restrictions.eq("u.name.norm", "cpt jack sparrow"))); c.add(Subqueries.propertyIn(mainAlias + ".oid", detached)); main.add(c); main.addOrder(Order.asc("u.name.orig")); String expected = HibernateToSqlTranslator.toSql(main); EqualFilter eqFilter = EqualFilter.createEqual(ObjectType.F_NAME, ObjectType.class, prismContext, null, new PolyString("cpt. Jack Sparrow", "cpt jack sparrow")); OrgFilter orgFilter = OrgFilter.createOrg("12341234-1234-1234-1234-123412341234"); ObjectQuery query = ObjectQuery.createObjectQuery(AndFilter.createAnd(eqFilter, orgFilter)); query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING)); String real = getInterpretedQuery(session, UserType.class, query); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.griffinslogistics.book.BookService.java
@Override public List<BookModel> getBookModelsByTransportId(Long transportId) { logger.log(Level.SEVERE, "{0}: getBookModelsByTransportation started", CLASS_NAME); List<BookModel> resultList = new ArrayList<BookModel>(); try {//from ww w.j a va 2 s. c o m DetachedCriteria criteria = DetachedCriteria.forClass(Book.class); criteria.add(Restrictions.eq("transportId", transportId)); criteria.setProjection( Projections.projectionList().add(Projections.groupProperty("bookNumber"), "bookNumber") .add(Projections.property("title"), "title")) .setResultTransformer(Transformers.aliasToBean(BookModel.class)); resultList = (List<BookModel>) this.dao.getAllByDetachedCriteria(criteria); Collections.reverse(resultList); } catch (HibernateException e) { logger.log(Level.SEVERE, e.getMessage()); } finally { logger.log(Level.SEVERE, "{0}: getBookModelsByTransportation finished", CLASS_NAME); } return resultList; }
From source file:com.hmsinc.epicenter.model.workflow.impl.WorkflowRepositoryImpl.java
License:Open Source License
/** * Get the subquery for filtering based on events. * /* w w w.j a v a 2 s .c o m*/ * @param geometry * @return */ private DetachedCriteria getInvestigationGeometryFilter(final Geometry geometry) { final DetachedCriteria dc = DetachedCriteria.forClass(Event.class, "event"); dc.createCriteria("event.investigations").add(Restrictions.eqProperty("id", "investigation.id")); dc.createCriteria("event.geography") .add(SpatialRestrictions.withinOrFilter("geometry", geometry, 1000, true)); dc.setProjection(Projections.property("id")); // dc.setComment(" */ /*+ ORDERED "); return dc; }
From source file:com.inkubator.hrm.dao.impl.EmpCareerHistoryDaoImpl.java
@Override public List<EmpCareerHistory> getByParamReport(ReportEmpMutationParameter searchParameter, int firstResult, int maxResults, Order order) { Criteria criteria = getCurrentSession().createCriteria(getEntityClass()); criteria.createAlias("bioData", "bioData", JoinType.INNER_JOIN); criteria.createAlias("jabatan", "jabatan", JoinType.INNER_JOIN); doSearchEmpRotasiByParamReport(searchParameter, criteria); DetachedCriteria maxTglPengangkatanQuery = DetachedCriteria.forClass(getEntityClass()); ProjectionList proj = Projections.projectionList(); proj.add(Projections.max("tglPenganngkatan")); proj.add(Projections.groupProperty("nik")); maxTglPengangkatanQuery.setProjection(proj); criteria.add(Subqueries.propertiesIn(new String[] { "tglPenganngkatan", "nik" }, maxTglPengangkatanQuery)); criteria.addOrder(order);/*from www . ja v a 2 s .c o m*/ criteria.setFirstResult(firstResult); criteria.setMaxResults(maxResults); List<EmpCareerHistory> listEmpCareerHistorys = criteria.list(); //Set Jabatan Lama/sebelumnya dari masing - masing record for (EmpCareerHistory ech : listEmpCareerHistorys) { Criteria criteriaOldPosition = getCurrentSession().createCriteria(getEntityClass()); criteriaOldPosition.setFetchMode("jabatan", FetchMode.JOIN); criteriaOldPosition.add(Restrictions.eq("nik", ech.getNik())); criteriaOldPosition.add(Restrictions.lt("tglPenganngkatan", ech.getTglPenganngkatan())); criteriaOldPosition.addOrder(Order.desc("tglPenganngkatan")); criteriaOldPosition.setMaxResults(1); EmpCareerHistory prevPosition = (EmpCareerHistory) criteriaOldPosition.uniqueResult(); //jika sebelumnya dia sudah pernah menjabat di posisi lain maka set oldJabatan dengan posisi tersebut if (null != prevPosition) { ech.setJabatanOldCode(prevPosition.getJabatan().getCode()); ech.setJabatanOldName(prevPosition.getJabatan().getName()); } else { ech.setJabatanOldCode("-"); } } return listEmpCareerHistorys; }
From source file:com.inkubator.hrm.dao.impl.EmpCareerHistoryDaoImpl.java
@Override public Long getTotalEmpCareerHistoryDataByParamReport(ReportEmpMutationParameter searchParameter) { Criteria criteria = getCurrentSession().createCriteria(getEntityClass()); doSearchEmpRotasiByParamReport(searchParameter, criteria); DetachedCriteria maxTglPengangkatanQuery = DetachedCriteria.forClass(getEntityClass()); ProjectionList proj = Projections.projectionList(); proj.add(Projections.max("tglPenganngkatan")); proj.add(Projections.groupProperty("nik")); maxTglPengangkatanQuery.setProjection(proj); criteria.add(Subqueries.propertiesIn(new String[] { "tglPenganngkatan", "nik" }, maxTglPengangkatanQuery)); return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult(); }
From source file:com.inkubator.hrm.dao.impl.LoanNewTypeDaoImpl.java
@Override public List<LoanNewType> getAllDataPayrollComponent(Long modelComponentId) { ProjectionList subProjection = Projections.projectionList(); subProjection.add(Projections.groupProperty("modelReffernsil")); DetachedCriteria subQuery = DetachedCriteria.forClass(PaySalaryComponent.class); subQuery.createAlias("modelComponent", "modelComponent", JoinType.INNER_JOIN); subQuery.add(Restrictions.eq("modelComponent.id", modelComponentId)); subQuery.setProjection(subProjection); Criteria criteria = getCurrentSession().createCriteria(getEntityClass()); criteria.add(Property.forName("id").notIn(subQuery)); return criteria.list(); }