List of usage examples for org.hibernate.criterion ProjectionList add
public ProjectionList add(Projection projection, String alias)
From source file:com.rta.vsd.data.service.impl.ViolationDataServiceImpl.java
/** * //from w ww. ja v a 2s . c o m * Gets a list of all the violations WithoutPlateConsficationInspections based on the plate details provided * * @author Eldon Barrows * @param dsContext * @param retrieveArabicData * @param vehiclePlateDetails * @param paginationValues * @return List<VsdViolation> * @throws VSDDataAccessException */ public List<VsdViolation> getViolationsWithoutPlateConsficationInspectionsByPlateDetails( final DataServiceContext dsContext, boolean retrieveArabicData, VehiclePlate vehiclePlateDetails, PaginationParam paginationValues) throws VSDDataAccessException { logger.info("getViolationsWithoutPlateConsficationInspectionsByPlateDetails -- START"); List<VsdViolation> violations = null; try { Session session = (Session) dsContext.getInternalContext(); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("violaton.violationId"), "violationId"); projectionList.add(Projections.property("violaton.reportedDate"), "reportedDate"); Criteria crit = session.createCriteria(VsdViolation.class, "violaton") .add(Restrictions.eq("violaton.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vsdInspections", "inspections", Criteria.LEFT_JOIN, Restrictions.eq("inspections.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vsdVehicleInfo", "vehicleInfo", Criteria.LEFT_JOIN, Restrictions.eq("vehicleInfo.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("vehicleInfo.vehiclePlateCategory", vehiclePlateDetails.getPlateCategory()) .ignoreCase()) .add(Restrictions.eq("vehicleInfo.vehiclePlateCode", vehiclePlateDetails.getPlateCode()) .ignoreCase()) .add(Restrictions.eq("vehicleInfo.vehiclePlateSource", vehiclePlateDetails.getPlateSource()) .ignoreCase()) .add(Restrictions.eq("vehicleInfo.vehiclePlateNumber", vehiclePlateDetails.getPlateNumber()) .ignoreCase()) .add(Restrictions.eq("inspections.isPlateConfiscated", Constant.FALSE)) .addOrder(Order.desc("violaton.reportedDate")) .setProjection(Projections.distinct(projectionList)); crit.setResultTransformer(new AliasToBeanResultTransformer(VsdViolation.class)); if (paginationValues.getFirstResult() != null && paginationValues.getFirstResult().longValue() != -1) { crit.setFirstResult(paginationValues.getFirstResult().intValue()); } if (paginationValues.getFetchedSize() != null && paginationValues.getFetchedSize().longValue() != -1) { crit.setMaxResults(paginationValues.getFetchedSize().intValue()); } List list = crit.list(); logger.debug("list.size() : " + list.size()); Set resultSet = new HashSet(list); logger.debug("resultSet.size() : " + resultSet.size()); if (resultSet.size() == 0) return new ArrayList(); Iterator iterator = resultSet.iterator(); ArrayList innerQueryList = new ArrayList<Long>(); while (iterator.hasNext()) { VsdViolation violation = (VsdViolation) iterator.next(); innerQueryList.add(violation.getViolationId()); } Criteria main = session.createCriteria(VsdViolation.class, "violaton") .add(Property.forName("violaton.violationId").in(innerQueryList)) .add(Restrictions.eq("violaton.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vsdInspections", "inspections", Criteria.LEFT_JOIN, Restrictions.eq("inspections.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vsdVehicleInfo", "vehicleInfo", Criteria.LEFT_JOIN, Restrictions.eq("vehicleInfo.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("vehicleInfo.vehiclePlateCategory", vehiclePlateDetails.getPlateCategory())) .add(Restrictions.eq("vehicleInfo.vehiclePlateCode", vehiclePlateDetails.getPlateCode())) .add(Restrictions.eq("vehicleInfo.vehiclePlateSource", vehiclePlateDetails.getPlateSource())) .add(Restrictions.eq("vehicleInfo.vehiclePlateNumber", vehiclePlateDetails.getPlateNumber())) .add(Restrictions.eq("inspections.isPlateConfiscated", Constant.FALSE)) .addOrder(Order.desc("violaton.reportedDate")); violations = main.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); logger.info("getViolationsWithoutPlateConsficationInspectionsByPlateDetails -- END"); return violations; } catch (Exception ex) { logger.error("An error occured in getViolationsWithoutPlateConsficationInspectionsByPlateDetails()"); throw new VSDDataAccessException(ex.getMessage(), ex); } }
From source file:com.rta.vsd.data.service.impl.ViolationDataServiceImpl.java
/** * /* w w w .j a v a 2 s. com*/ * Gets all the violations WithoutPlateConsfication for vehicles owned by the owner with the specified trade license number * * @author Eldon Barrows * @param dsContext * @param retrieveArabicData * @param param * @return List<VsdViolation> * @throws VSDDataAccessException */ public List<VsdViolation> getViolationsWithoutPlateConsficationByTraficFileNumber( final DataServiceContext dsContext, boolean retrieveArabicData, String traficFileNo, PaginationParam param) throws VSDDataAccessException { logger.info("getViolationsWithoutPlateConsficationByTraficFileNumber -- START"); List<VsdViolation> violations = null; try { Session session = (Session) dsContext.getInternalContext(); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.property("v.violationId"), "violationId"); projectionList.add(Projections.property("v.reportedDate"), "reportedDate"); violations = session.createCriteria(VsdViolation.class, "v") .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("v.vsdInspections", "i", Criteria.LEFT_JOIN, Restrictions.eq("i.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("i.isPlateConfiscated", Constant.FALSE)) .createCriteria("i.vsdVehicleInfo", "vi", Criteria.LEFT_JOIN, Restrictions.eq("vi.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vi.vsdOwnerInfos", "oi", Criteria.LEFT_JOIN, Restrictions.eq("oi.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("oi.trafficFileNumber", traficFileNo).ignoreCase()) .createCriteria("v.vsdSeverityLevel", "vsl", Criteria.LEFT_JOIN, Restrictions.eq("vsl.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("v.vsdViolationStatus", "vs", Criteria.LEFT_JOIN, Restrictions.eq("vs.isDeleted", IDataService.BOOL_FALSE)) .addOrder(Order.desc("v.reportedDate")).setProjection(Projections.distinct(projectionList)) .setFirstResult(param.getFirstResult().intValue()) .setMaxResults(param.getFetchedSize().intValue()) .setResultTransformer(new AliasToBeanResultTransformer(VsdViolation.class)).list(); if (violations == null || violations.size() == 0) return new ArrayList(); Iterator iterator = violations.iterator(); ArrayList innerQueryList = new ArrayList<Long>(); while (iterator.hasNext()) { VsdViolation violation = (VsdViolation) iterator.next(); innerQueryList.add(violation.getViolationId()); } violations = session.createCriteria(VsdViolation.class, "v") .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("v.vsdInspections", "i", Criteria.LEFT_JOIN, Restrictions.eq("i.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("i.isPlateConfiscated", Constant.FALSE)) .createCriteria("i.vsdVehicleInfo", "vi", Criteria.LEFT_JOIN, Restrictions.eq("vi.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("vi.vsdOwnerInfos", "oi", Criteria.LEFT_JOIN, Restrictions.eq("oi.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.eq("oi.trafficFileNumber", traficFileNo).ignoreCase()) .createCriteria("v.vsdSeverityLevel", "vsl", Criteria.LEFT_JOIN, Restrictions.eq("vsl.isDeleted", IDataService.BOOL_FALSE)) .createCriteria("v.vsdViolationStatus", "vs", Criteria.LEFT_JOIN, Restrictions.eq("vs.isDeleted", IDataService.BOOL_FALSE)) .add(Restrictions.in("v.violationId", innerQueryList)).addOrder(Order.desc("v.reportedDate")) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); logger.info("getViolationsWithoutPlateConsficationByTraficFileNumber -- END"); return violations; } catch (Exception ex) { logger.error("An error occured in getViolationsWithoutPlateConsficationByTraficFileNumber()"); logger.error(ex); throw new VSDDataAccessException(ex.getMessage(), ex); } }
From source file:com.ut.tekir.finance.CollectionBrowseBean.java
License:LGPL
@Override public DetachedCriteria buildCriteria() { DetachedCriteria crit = DetachedCriteria.forClass(Payment.class); crit.createAlias("this.contact", "contact"); crit.createAlias("this.account", "account"); ProjectionList pl = Projections.projectionList(); pl.add(Projections.property("this.id"), "id").add(Projections.property("this.serial"), "serial") .add(Projections.property("this.reference"), "reference") .add(Projections.property("this.code"), "code").add(Projections.property("this.date"), "date") .add(Projections.property("this.info"), "info") .add(Projections.property("contact.code"), "contactCode") .add(Projections.property("contact.fullname"), "contactName") .add(Projections.property("contact.company"), "company") .add(Projections.property("contact.person"), "person") .add(Projections.property("account.code"), "accountCode") .add(Projections.property("this.totalAmount.currency"), "totalAmountCurrency") .add(Projections.property("this.totalAmount.value"), "totalAmountValue"); if (filterModel.getWorkBunch() != null) { crit.createAlias("this.items", "it", CriteriaSpecification.LEFT_JOIN); pl.add(Projections.property("it.workBunch"), "workBunch"); pl.add(Projections.property("it.amount.value"), "itemAmountValue"); crit.add(Restrictions.eq("it.workBunch", filterModel.getWorkBunch())); }//from w w w . ja v a 2 s . com crit.setProjection(pl).setResultTransformer(Transformers.aliasToBean(PaymentFilterModel.class)); if (isNotEmpty(filterModel.getSerial())) { crit.add(Restrictions.ilike("this.serial", filterModel.getSerial(), MatchMode.START)); } if (isNotEmpty(filterModel.getReference())) { crit.add(Restrictions.ilike("this.reference", filterModel.getReference(), MatchMode.START)); } if (isNotEmpty(filterModel.getCode())) { crit.add(Restrictions.ilike("this.code", filterModel.getCode(), MatchMode.START)); } if (filterModel.getBeginDate() != null) { crit.add(Restrictions.ge("this.date", filterModel.getBeginDate())); } if (filterModel.getEndDate() != null) { crit.add(Restrictions.le("this.date", filterModel.getEndDate())); } if (isNotEmpty(filterModel.getContactCode())) { crit.add(Restrictions.ilike("contact.code", filterModel.getContactCode(), MatchMode.START)); } if (isNotEmpty(filterModel.getContactName())) { crit.add(Restrictions.ilike("contact.fullname", filterModel.getContactName(), MatchMode.START)); } if (filterModel.getProcessType() != null) { crit.add(Restrictions.eq("this.processType", filterModel.getProcessType())); } crit.add(Restrictions.eq("this.action", FinanceAction.Credit)); crit.addOrder(Order.desc("this.date")); crit.addOrder(Order.desc("this.serial")); return crit; }
From source file:com.ut.tekir.finance.PaymentBrowseBean.java
License:LGPL
@Override public DetachedCriteria buildCriteria() { DetachedCriteria crit = DetachedCriteria.forClass(Payment.class); crit.createAlias("this.contact", "contact"); crit.createAlias("this.account", "account"); ProjectionList pl = Projections.projectionList(); pl.add(Projections.property("this.id"), "id").add(Projections.property("this.serial"), "serial") .add(Projections.property("this.reference"), "reference") .add(Projections.property("this.code"), "code").add(Projections.property("this.date"), "date") .add(Projections.property("this.info"), "info") .add(Projections.property("contact.code"), "contactCode") .add(Projections.property("contact.name"), "contactName") .add(Projections.property("contact.company"), "company") .add(Projections.property("contact.person"), "person") .add(Projections.property("account.code"), "accountCode") .add(Projections.property("this.totalAmount.currency"), "totalAmountCurrency") .add(Projections.property("this.totalAmount.value"), "totalAmountValue"); if (filterModel.getWorkBunch() != null) { crit.createAlias("this.items", "it", CriteriaSpecification.LEFT_JOIN); pl.add(Projections.property("it.workBunch"), "workBunch"); pl.add(Projections.property("it.amount.value"), "itemAmountValue"); crit.add(Restrictions.eq("it.workBunch", filterModel.getWorkBunch())); }//from ww w.j a va2 s . c o m crit.setProjection(pl).setResultTransformer(Transformers.aliasToBean(PaymentFilterModel.class)); /* crit.setProjection(Projections.projectionList() .add(Projections.property("this.id"), "id") .add(Projections.property("this.serial"), "serial") .add(Projections.property("this.reference"), "reference") .add(Projections.property("this.code"), "code") .add(Projections.property("this.date"), "date") .add(Projections.property("this.info"), "info") .add(Projections.property("contact.code"), "contactCode") .add(Projections.property("contact.name"), "contactName") .add(Projections.property("contact.company"), "company") .add(Projections.property("contact.person"),"person") .add(Projections.property("account.code"), "accountCode") .add(Projections.property("this.totalAmount.currency"), "totalAmountCurrency") .add(Projections.property("this.totalAmount.value"), "totalAmountValue") ) .setResultTransformer(Transformers.aliasToBean(PaymentFilterModel.class)); */ if (isNotEmpty(filterModel.getSerial())) { crit.add(Restrictions.ilike("this.serial", filterModel.getSerial(), MatchMode.START)); } if (isNotEmpty(filterModel.getReference())) { crit.add(Restrictions.ilike("this.reference", filterModel.getReference(), MatchMode.START)); } if (isNotEmpty(filterModel.getCode())) { crit.add(Restrictions.ilike("this.code", filterModel.getCode(), MatchMode.START)); } if (filterModel.getBeginDate() != null) { crit.add(Restrictions.ge("this.date", filterModel.getBeginDate())); } if (filterModel.getEndDate() != null) { crit.add(Restrictions.le("this.date", filterModel.getEndDate())); } if (isNotEmpty(filterModel.getContactCode())) { crit.add(Restrictions.ilike("contact.code", filterModel.getContactCode(), MatchMode.START)); } if (isNotEmpty(filterModel.getContactName())) { crit.add(Restrictions.ilike("contact.fullname", filterModel.getContactName(), MatchMode.START)); } if (filterModel.getProcessType() != null) { crit.add(Restrictions.eq("this.processType", filterModel.getProcessType())); } crit.add(Restrictions.eq("this.action", FinanceAction.Debit)); crit.addOrder(Order.desc("this.date")); crit.addOrder(Order.desc("serial")); return crit; }
From source file:com.ut.tekir.stock.PriceItemBrowseBean.java
License:LGPL
@Override @SuppressWarnings("unchecked") public void search() { log.debug("Search Execute"); HibernateSessionProxy session = (HibernateSessionProxy) getEntityManager().getDelegate(); Criteria ecrit = buildCriteria().getExecutableCriteria(session); //ecrit.setCacheable(true); ecrit.setMaxResults(100);/*from w w w.jav a2 s . c om*/ ProjectionList pl = Projections.projectionList().add(Projections.property("this.id"), "id") .add(Projections.property("this.active"), "active") .add(Projections.property("this.defaultItem"), "defaultItem") .add(Projections.property("this.info"), "info").add(Projections.property("this.code"), "code") .add(Projections.property("this.endDate"), "endDate") .add(Projections.property("this.beginDate"), "beginDate") .add(Projections.property("this.action"), "action"); ecrit.createAlias("this.owner", "owner", DetachedCriteria.LEFT_JOIN); pl.add(Projections.property("owner.code"), "ownerCode").add(Projections.property("owner.listName"), "ownerListName"); if (filterModel.getPriceList() != null) { ecrit.add(Restrictions.eq("owner", filterModel.getPriceList())); } ecrit.createAlias("this.group", "group", DetachedCriteria.LEFT_JOIN); pl.add(Projections.property("group"), "group"); if (filterModel.getGroup() != null) { ecrit.add(Restrictions.eq("this.group", filterModel.getGroup())); } ecrit.setProjection(pl).setResultTransformer(Transformers.aliasToBean(PriceItemFilterModel.class)); setEntityList(ecrit.list()); }
From source file:edu.utah.further.core.data.util.HibernateUtil.java
License:Apache License
/** * @deprecated This method does not work well due to Hibernate bug HHH-817, nor does * AliasToBeanResultTransformer handle multi-level property values. * Therefore it should not be used. * * @param propertyNames// w w w.j ava2 s . c o m * @param alias * @param aliasPath * @param domainClass * @return * * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-817 */ @Deprecated public static Projection createAliasedProjectionList(final String[] propertyNames, final String alias, final String aliasPath, final Class<?> domainClass) { final ProjectionList projectionList = Projections.projectionList(); for (final String propertyName : propertyNames) { final Field field = ReflectionUtils.findField(domainClass, propertyName); if (!hasAssociationAnnotation(field)) { final String aliasedProperty = alias + "." + propertyName; projectionList.add(Projections.property(aliasedProperty), aliasedProperty); } } return projectionList; }
From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateDistinctEntityExecutor.java
License:Apache License
@Override public boolean process(final ChainRequest request) { final HibernateExecReq execReq = new HibernateExecReq(request); final GenericCriteria criteria = execReq.getResult(); notNull(criteria, "Expected Hibernate criteria"); final Class<? extends PersistentEntity<?>> domainClass = execReq.getRootEntity(); final SessionFactory sessionFactory = execReq.getSessionFactory(); notNull(sessionFactory, "Expected SessionFactory"); // Get information about the root entity class final ClassMetadata classMetadata = sessionFactory.getClassMetadata(domainClass); final String[] properties = classMetadata.getPropertyNames(); final String identifierName = classMetadata.getIdentifierPropertyName(); final ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.distinct(Projections.property(identifierName)), identifierName); // When you use projections, you have to manually specify the selection criteria // so we loop through all the properties and specify them here. Note that we skip // all the relationship properties (collections). for (final String property : properties) { final Type type = classMetadata.getPropertyType(property); if (!type.isCollectionType()) { projectionList.add(Projections.property(property), property); }/*from www.j av a2 s. c o m*/ } criteria.setProjection(projectionList); // This turns all of the results into the actual root entity class - calling // setters/etc criteria.setResultTransformer(new AliasToBeanResultTransformer(domainClass)); execReq.setResult(criteria); return false; }
From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateDistinctIdExecutor.java
License:Apache License
/** * @param request//from ww w. j av a2s.c om * @return * @see edu.utah.further.core.chain.AbstractRequestHandler#process(edu.utah.further.core.api.chain.ChainRequest) * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-817 */ @Override public boolean process(final ChainRequest request) { final HibernateExecReq executionReq = new HibernateExecReq(request); // Validate required input final GenericCriteria hibernateCriteria = executionReq.getResult(); notNull(hibernateCriteria, "Expected Hibernate criteria"); final Class<? extends PersistentEntity<?>> domainClass = executionReq.getRootEntity(); final Class<? extends PersistentEntity<?>> entityClass = dao.getEntityClass(domainClass); notNull(entityClass, "Expected root entity class"); final SessionFactory sessionFactory = executionReq.getSessionFactory(); notNull(sessionFactory, "Expected SessionFactory"); final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass); final String identifierName = classMetadata.getIdentifierPropertyName(); final Type identifierType = classMetadata.getIdentifierType(); // A hack to obtain projections out of the critieria by casting to the Hibernate // implementation. TODO: improve adapter to do that via interface access final ProjectionList projectionList = Projections.projectionList(); final Projection existingProjection = ((CriteriaImpl) hibernateCriteria.getHibernateCriteria()) .getProjection(); if (existingProjection != null && !overrideExistingProjection) { return false; } if (identifierType.isComponentType()) { final ComponentType componentType = (ComponentType) identifierType; final String[] idPropertyNames = componentType.getPropertyNames(); // Add distinct to the first property projectionList.add( Projections .distinct(Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[0])), idPropertyNames[0]); // Add the remaining properties to the projection list for (int i = 1; i < idPropertyNames.length; i++) { projectionList.add(Property.forName(identifierName + PROPERTY_SCOPE_CHAR + idPropertyNames[i]), idPropertyNames[i]); } hibernateCriteria.setProjection(projectionList); hibernateCriteria.setResultTransformer(new AliasToBeanResultTransformer( ReflectionUtils.findField(entityClass, identifierName).getType())); } else { // 'this' required to avoid HHH-817 projectionList.add(Projections.distinct(Property.forName(THIS_CONTEXT + identifierName))); hibernateCriteria.setProjection(projectionList); } executionReq.setResult(hibernateCriteria); return false; }
From source file:ee.ria.xroad.opmonitordaemon.OperationalDataRecordManager.java
License:Open Source License
private static void setProjectionList(Criteria criteria, Set<String> fields) { ProjectionList projList = Projections.projectionList(); HashSet<String> fieldSet = new HashSet<>(fields); // Necessary for searching the records. fieldSet.add(MONITORING_DATA_TS);//from w ww. jav a 2s. c o m log.trace("setProjectionList(): {}", fieldSet); fieldSet.forEach(i -> projList.add(Projections.property(i), i)); criteria.setProjection(projList); criteria.setResultTransformer(Transformers.aliasToBean(OperationalDataRecord.class)); }
From source file:eu.jangos.realm.controller.characters.ItemInstanceService.java
License:Apache License
/** * Return only the necessary fields of the equipment of the character in parameter for the CharEnum packet. * * @param character// www . j a va 2 s. c o m * @return A List of arrays of Object with the following structure: [slot,entry] where slot is the slot * into which this item is located and entry is the ID of the item_template. */ public List getEquipmentCharEnum(Characters character) { logger.debug("getEquipment " + character.getName()); try (Session session = HibernateUtil.getCharSession().openSession()) { ProjectionList proList = Projections.projectionList(); proList.add(Projections.property("slot"), "slot"); proList.add(Projections.property("fkObjectEntry"), "entry"); return session.createCriteria(ItemInstance.class).setProjection(proList) .add(Restrictions.and(Restrictions.eq("characters.guid", character.getGuid()), Restrictions.eq("itemStorageType.id", ItemStorageEnum.EQUIPPED.getValue()))) .addOrder(Order.asc("slot")).list(); } catch (HibernateException he) { logger.debug("There was an error querying the database."); return null; } }