List of usage examples for org.hibernate.criterion DetachedCriteria setResultTransformer
public DetachedCriteria setResultTransformer(ResultTransformer resultTransformer)
From source file:org.opennms.netmgt.dao.hibernate.AssetRecordDaoHibernate.java
License:Open Source License
@Override public List<OnmsAssetRecord> getDistinctProperties() { DetachedCriteria criteria = DetachedCriteria.forClass(OnmsAssetRecord.class); ProjectionList projList = Projections.projectionList(); // projList.add(Projections.alias(Projections.property("geolocation"), "geolocation")); projList.add(Projections.alias(Projections.property("additionalhardware"), "additionalhardware")); projList.add(Projections.alias(Projections.property("geolocation.address1"), "address1")); projList.add(Projections.alias(Projections.property("geolocation.address2"), "address2")); projList.add(Projections.alias(Projections.property("admin"), "admin")); projList.add(Projections.alias(Projections.property("assetNumber"), "assetNumber")); projList.add(Projections.alias(Projections.property("autoenable"), "autoenable")); projList.add(Projections.alias(Projections.property("building"), "building")); projList.add(Projections.alias(Projections.property("category"), "category")); projList.add(Projections.alias(Projections.property("circuitId"), "circuitId")); projList.add(Projections.alias(Projections.property("geolocation.city"), "city")); projList.add(Projections.alias(Projections.property("comment"), "comment")); projList.add(Projections.alias(Projections.property("connection"), "connection")); projList.add(Projections.alias(Projections.property("geolocation.longitude"), "longitude")); projList.add(Projections.alias(Projections.property("geolocation.latitude"), "latitude")); projList.add(Projections.alias(Projections.property("cpu"), "cpu")); projList.add(Projections.alias(Projections.property("department"), "department")); projList.add(Projections.alias(Projections.property("description"), "description")); projList.add(Projections.alias(Projections.property("displayCategory"), "displayCategory")); projList.add(Projections.alias(Projections.property("division"), "division")); projList.add(Projections.alias(Projections.property("enable"), "enable")); projList.add(Projections.alias(Projections.property("floor"), "floor")); projList.add(Projections.alias(Projections.property("hdd1"), "hdd1")); projList.add(Projections.alias(Projections.property("hdd2"), "hdd2")); projList.add(Projections.alias(Projections.property("hdd3"), "hdd3")); projList.add(Projections.alias(Projections.property("hdd4"), "hdd4")); projList.add(Projections.alias(Projections.property("hdd5"), "hdd5")); projList.add(Projections.alias(Projections.property("hdd6"), "hdd6")); projList.add(Projections.alias(Projections.property("inputpower"), "inputpower")); projList.add(Projections.alias(Projections.property("lease"), "lease")); projList.add(Projections.alias(Projections.property("maintcontract"), "maintcontract")); projList.add(Projections.alias(Projections.property("manufacturer"), "manufacturer")); projList.add(Projections.alias(Projections.property("modelNumber"), "modelNumber")); projList.add(Projections.alias(Projections.property("notifyCategory"), "notifyCategory")); projList.add(Projections.alias(Projections.property("numpowersupplies"), "numpowersupplies")); projList.add(Projections.alias(Projections.property("operatingSystem"), "operatingSystem")); projList.add(Projections.alias(Projections.property("pollerCategory"), "pollerCategory")); projList.add(Projections.alias(Projections.property("port"), "port")); projList.add(Projections.alias(Projections.property("rack"), "rack")); projList.add(Projections.alias(Projections.property("ram"), "ram")); projList.add(Projections.alias(Projections.property("region"), "region")); projList.add(Projections.alias(Projections.property("room"), "room")); projList.add(Projections.alias(Projections.property("serialNumber"), "serialNumber")); projList.add(Projections.alias(Projections.property("slot"), "slot")); projList.add(Projections.alias(Projections.property("snmpcommunity"), "snmpcommunity")); projList.add(Projections.alias(Projections.property("geolocation.state"), "state")); projList.add(Projections.alias(Projections.property("storagectrl"), "storagectrl")); projList.add(Projections.alias(Projections.property("supportPhone"), "supportPhone")); projList.add(Projections.alias(Projections.property("thresholdCategory"), "thresholdCategory")); projList.add(Projections.alias(Projections.property("username"), "username")); projList.add(Projections.alias(Projections.property("vendor"), "vendor")); projList.add(Projections.alias(Projections.property("vendorAssetNumber"), "vendorAssetNumber")); projList.add(Projections.alias(Projections.property("vendorFax"), "vendorFax")); projList.add(Projections.alias(Projections.property("vendorPhone"), "vendorPhone")); projList.add(Projections.alias(Projections.property("geolocation.zip"), "zip")); projList.add(Projections.alias(Projections.property("vmwareManagedObjectId"), "vmwareManagedObjectId")); projList.add(Projections.alias(Projections.property("vmwareManagedEntityType"), "vmwareManagedEntityType")); projList.add(Projections.alias(Projections.property("vmwareManagementServer"), "vmwareManagementServer")); projList.add(Projections.alias(Projections.property("vmwareTopologyInfo"), "vmwareTopologyInfo")); projList.add(Projections.alias(Projections.property("vmwareState"), "vmwareState")); criteria.setProjection(Projections.distinct(projList)); criteria.setResultTransformer(Transformers.aliasToBean(OnmsAssetRecord.class)); @SuppressWarnings("unchecked") List<OnmsAssetRecord> result = (List<OnmsAssetRecord>) getHibernateTemplate().findByCriteria(criteria); return result; }
From source file:org.tonguetied.keywordmanagement.KeywordRepositoryImpl.java
License:Apache License
public PaginatedList<Keyword> findKeywords(Keyword keyword, final boolean ignoreCase, final Order order, final Integer firstResult, final Integer maxResults) throws IllegalArgumentException { if (keyword == null) { throw new IllegalArgumentException("keyword cannot be null"); }/*from w w w .j av a 2 s . co m*/ final MatchMode matchMode = MatchMode.ANYWHERE; Example criterionKeyword = Example.create(keyword); criterionKeyword.enableLike(matchMode); if (ignoreCase) { criterionKeyword.ignoreCase(); } // Normally, Hibernate performs a left-outer join, when searching for // an object with collections using Criteria. This returns a ResultSet // that contains duplicate objects. In order to get a unique list of // Keywords with paginated support, we need to a nested query to find // distinct matching ids, then get the Keywords. The end result is a // subselect in the main query, but only one query is sent. DetachedCriteria dc = DetachedCriteria.forClass(Keyword.class); dc.add(criterionKeyword); dc.setResultTransformer(DISTINCT_ROOT_ENTITY); Conjunction conjunction = createTranslationConditions(keyword.getTranslations(), ignoreCase, matchMode); if (conjunction != null) dc.createCriteria(FIELD_TRANSLATIONS).add(conjunction); dc.setProjection(Projections.id()); Criteria criteria = getSession().createCriteria(Keyword.class); criteria.add(Subqueries.propertyIn(FIELD_ID, dc)); if (Order.desc == order) criteria.addOrder(desc(FIELD_KEYWORD)); else criteria.addOrder(asc(FIELD_KEYWORD)); if (firstResult != null) criteria.setFirstResult(firstResult); if (maxResults != null) criteria.setMaxResults(maxResults); final List<Keyword> criteriaList = criteria.list(); int maxListSize = 0; if (criteriaList.size() > 0) { maxListSize = calculateMaxListSize(criterionKeyword, conjunction); } return new PaginatedList<Keyword>(criteriaList, maxListSize); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public <T> List<T> getInstances(Class<T> type, DetachedCriteria criteria) { criteria = alterCriteria(type, criteria); criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); return criteria.getExecutableCriteria(getSession()).list(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public List getInstances(final Object example, final TynamoClassDescriptor classDescriptor) { //create Criteria instance DetachedCriteria searchCriteria = DetachedCriteria.forClass(example.getClass()); searchCriteria = alterCriteria(example.getClass(), searchCriteria); //loop over the example object's PropertyDescriptors for (TynamoPropertyDescriptor propertyDescriptor : classDescriptor.getPropertyDescriptors()) { //only add a Criterion to the Criteria instance if this property is searchable if (propertyDescriptor.isSearchable()) { String propertyName = propertyDescriptor.getName(); Class propertyClass = propertyDescriptor.getPropertyType(); Object value = getPropertyAccess().get(example, propertyName); //only add a Criterion to the Criteria instance if the value for this property is non-null if (value != null) { if (String.class.isAssignableFrom(propertyClass) && ((String) value).length() > 0) { searchCriteria.add(Restrictions.like(propertyName, value.toString(), MatchMode.ANYWHERE)); }/* w ww. ja v a 2 s .com*/ /** * 'one'-end of many-to-one, one-to-one * * Just match the identifier */ else if (propertyDescriptor.isObjectReference()) { Serializable identifierValue = getIdentifier(value, descriptorService.getClassDescriptor(propertyDescriptor.getBeanType())); searchCriteria.createCriteria(propertyName).add(Restrictions.idEq(identifierValue)); } else if (propertyClass.isPrimitive()) { //primitive types: ignore zeroes in case of numeric types, ignore booleans anyway (TODO come up with something...) if (!propertyClass.equals(boolean.class) && ((Number) value).longValue() != 0) { searchCriteria.add(Restrictions.eq(propertyName, value)); } } else if (propertyDescriptor.isCollection()) { //one-to-many or many-to-many CollectionDescriptor collectionDescriptor = (CollectionDescriptor) propertyDescriptor; TynamoClassDescriptor collectionClassDescriptor = descriptorService .getClassDescriptor(collectionDescriptor.getElementType()); if (collectionClassDescriptor != null) { String identifierName = collectionClassDescriptor.getIdentifierDescriptor().getName(); Collection<Serializable> identifierValues = new ArrayList<Serializable>(); Collection associatedItems = (Collection) value; if (associatedItems != null && associatedItems.size() > 0) { for (Object o : associatedItems) { identifierValues.add(getIdentifier(o, collectionClassDescriptor)); } //add a 'value IN collection' restriction searchCriteria.createCriteria(propertyName) .add(Restrictions.in(identifierName, identifierValues)); } } } } } } searchCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); // FIXME This won't work because the shadow proxy doesn't implement SessionImplementor // that session is casted to. Maybe we should inject SessionManager instead // and obtain the Session from it return searchCriteria.getExecutableCriteria(getSession()).list(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public int count(Class type, DetachedCriteria detachedCriteria) { detachedCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); final DetachedCriteria criteria = alterCriteria(type, detachedCriteria); Criteria executableCriteria = criteria.getExecutableCriteria(getSession()) .setProjection(Projections.rowCount()); return ((Long) executableCriteria.uniqueResult()).intValue(); }
From source file:org.tynamo.hibernate.services.HibernatePersistenceServiceImpl.java
License:Apache License
public List getInstances(final DetachedCriteria detachedCriteria, final int startIndex, final int maxResults) { detachedCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY); Criteria executableCriteria = detachedCriteria.getExecutableCriteria(getSession()); if (startIndex >= 0) { executableCriteria.setFirstResult(startIndex); }// w ww . j a v a 2 s. c om if (maxResults > 0) { executableCriteria.setMaxResults(maxResults); } return executableCriteria.list(); }
From source file:org.web4thejob.orm.DataReaderServiceImpl.java
License:Open Source License
private DetachedCriteria toDetachedCriteria(Query query, String alias) { boolean hasOneToManyAssociation = false; DetachedCriteria detachedCriteria; if (alias != null) { detachedCriteria = DetachedCriteria.forClass(query.getTargetType(), alias); } else {/*from ww w.jav a 2 s . c o m*/ detachedCriteria = DetachedCriteria.forClass(query.getTargetType()); } final Map<String, String> aliases = new HashMap<String, String>(); for (final Criterion w4tjCriterion : query.getCriteria()) { if (w4tjCriterion.getCondition() != null && (w4tjCriterion.getCondition().getOperandsNo() == 0 || (w4tjCriterion.getValue() != null && StringUtils.hasText(w4tjCriterion.getValue().toString())))) { if (!hasOneToManyAssociation) { hasOneToManyAssociation = w4tjCriterion.getPropertyPath().hasOneToManySteps(); } org.hibernate.criterion.Criterion hibCriterion; if (w4tjCriterion.isLocal()) { hibCriterion = toHibernateCriterion(w4tjCriterion, detachedCriteria.getAlias()); } else { String aliasPath = null; for (final PropertyMetadata propertyMetadata : w4tjCriterion.getPropertyPath().getSteps()) { if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getLastStep())) { break; } else if (propertyMetadata.equals(w4tjCriterion.getPropertyPath().getFirstStep())) { aliasPath = propertyMetadata.getName(); } else { aliasPath += "." + propertyMetadata.getName(); } buildAlias(detachedCriteria, aliases, aliasPath); } hibCriterion = toHibernateCriterion(w4tjCriterion, aliases.get(aliasPath)); } detachedCriteria = detachedCriteria.add(hibCriterion); } } if (!query.getSubqueries().isEmpty()) { String masterId = detachedCriteria.getAlias() + "." + ContextUtil.getMRS().getEntityMetadata(query.getTargetType()).getIdentifierName(); int subqindex = 0; for (Subquery subquery : query.getSubqueries()) { subqindex += 1; detachedCriteria = detachedCriteria .add(toHibernateSubcriterion(masterId, String.valueOf(subqindex), subquery)); } } for (final OrderBy orderBy : query.getOrderings()) { PathMetadata pathMetadata = ContextUtil.getMRS().getPropertyPath(query.getTargetType(), StringUtils.delimitedListToStringArray(orderBy.getProperty(), Path.DELIMITER)); String property; if (pathMetadata.isMultiStep()) { String aliasPath = null; if (!hasOneToManyAssociation) { hasOneToManyAssociation = pathMetadata.hasOneToManySteps(); } for (PropertyMetadata propertyMetadata : pathMetadata.getSteps()) { if (propertyMetadata.equals(pathMetadata.getLastStep())) { break; } else if (propertyMetadata.equals(pathMetadata.getFirstStep())) { aliasPath = propertyMetadata.getName(); } else { aliasPath += "." + propertyMetadata.getName(); } buildAlias(detachedCriteria, aliases, aliasPath); } property = aliases.get(aliasPath) + "." + pathMetadata.getLastStep().getName(); } else { property = orderBy.getProperty(); } if (orderBy.isDescending()) { detachedCriteria = detachedCriteria.addOrder(Order.desc(property)); } else { detachedCriteria = detachedCriteria.addOrder(Order.asc(property)); } } if (hasOneToManyAssociation) { detachedCriteria = detachedCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); } return detachedCriteria; }
From source file:pula.sys.daos.impl.CourseClientDaoImpl.java
@Override public PaginationSupport<MapBean> search(CourseClientCondition condition, int pageIndex) { DetachedCriteria dc = makeDetachedCriteria(condition); ProjectionList proList = Projections.projectionList();// ? proList = HibernateTool.injectSingle(proList, SINGLE_MAPPING, "uu"); proList = HibernateTool.injectAlias(proList, ALIAS_MAPPING); dc.setProjection(proList);/*ww w. java 2 s . c o m*/ dc.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); String dateField = "uu.applyTime"; if (condition.isForApply()) { dateField = "uu.createdTime"; } PaginationSupport<Map<String, Object>> es = super.findPageByCriteria(dc, new PageInfo(pageIndex), Order.desc(dateField)); return MapList.createPage(es); }
From source file:pula.sys.daos.impl.CourseClientDaoImpl.java
@Override public MapBean unique(long id) { CourseClientCondition condition = new CourseClientCondition(); condition.setId(id);/*from w w w.java 2s . co m*/ DetachedCriteria dc = makeDetachedCriteria(condition); ProjectionList proList = Projections.projectionList();// ? proList = HibernateTool.injectSingle(proList, SINGLE_MAPPING, "uu"); proList = HibernateTool.injectSingle(proList, SINGLE_MAPPING_EXT, "uu"); proList = HibernateTool.injectAlias(proList, ALIAS_MAPPING); dc.setProjection(proList); dc.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); Map<String, Object> map = super.uniqueResult(dc); return MapBean.map(map); }
From source file:sernet.gs.ui.rcp.main.service.crudcommands.LoadNotes.java
License:Open Source License
public void execute() { if (getLog().isDebugEnabled()) { getLog().debug("executing, id is: " + getCnAElementId() + "..."); }/*from w ww. ja va2 s . c o m*/ if (getCnAElementId() != null) { IBaseDao<Note, Serializable> dao = getDaoFactory().getDAO(Note.class); DetachedCriteria crit = DetachedCriteria.forClass(Note.class); crit.add(Restrictions.eq("cnATreeElementId", getCnAElementId())); crit.setFetchMode("entity", FetchMode.JOIN); crit.setFetchMode("entity.typedPropertyLists", FetchMode.JOIN); crit.setFetchMode("entity.typedPropertyLists.properties", FetchMode.JOIN); crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); List<Note> internalNoteList = dao.findByCriteria(crit); if (getLog().isDebugEnabled()) { getLog().debug("number of notes found: " + internalNoteList.size()); } for (Note note : internalNoteList) { Entity entity = note.getEntity(); if (entity != null) { for (PropertyList pl : entity.getTypedPropertyLists().values()) { for (Property p : pl.getProperties()) { p.setParent(entity); } } } } Collections.sort(internalNoteList, sorter); setNoteList(internalNoteList); } }