List of usage examples for org.hibernate.criterion Projections id
public static IdentifierProjection id()
From source file:com.sapienter.jbilling.server.payment.db.PaymentDAS.java
License:Open Source License
public List<Integer> findIdsByUserLatestFirst(Integer userId, int maxResults) { Criteria criteria = getSession().createCriteria(PaymentDTO.class).add(Restrictions.eq("deleted", 0)) .createAlias("baseUser", "u").add(Restrictions.eq("u.id", userId)).setProjection(Projections.id()) .addOrder(Order.desc("id")).setMaxResults(maxResults); return criteria.list(); }
From source file:com.sapienter.jbilling.server.process.db.BillingProcessDAS.java
License:Open Source License
public ScrollableResults findUsersToProcess(int entityId) { Criteria criteria = getSession().createCriteria(UserDTO.class).add(Restrictions.eq("deleted", 0)) .createAlias("company", "c").add(Restrictions.eq("c.id", entityId)).createAlias("userStatus", "us") .add(Restrictions.lt("us.id", UserDTOEx.STATUS_SUSPENDED)).setProjection(Projections.id()) .setComment("BillingProcessDAS.findUsersToProcess " + entityId); return criteria.scroll(); }
From source file:com.thesett.catalogue.impl.standalone.CatalogueManagerServiceImpl.java
License:Apache License
/** {@inheritDoc} */ public PagingResult executePagedQuery(int from, int number, String databaseEntityName, String entityTypeName, String viewTypeName, Criterion criterion, Map<String, Criterion> joins) { log.debug("public PagingResult executePagedQuery(int from = " + from + ", int number = " + number + ", String databaseEntityName = " + databaseEntityName + ", String entityTypeName = " + entityTypeName + ", String viewTypeName = " + viewTypeName + ", Criterion criterion, " + "Map<String, Criterion> joins): called"); Session session = HibernateUtil.getCurrentSession(); HibernateUtil.beginTransaction();/* w w w .ja v a 2s. c o m*/ // Project the id and external id properties and just the remaining properties that are required to project // the results onto the specified view type. ProjectionList properties = Projections.projectionList().add(Projections.id()) .add(Property.forName("externalId")); ViewType viewType = getCatalogue().getViewType(viewTypeName); for (String fieldName : viewType.getAllPropertyTypes().keySet()) { properties.add(Property.forName(fieldName)); } // Create the selection criteria for the block. Criteria selectCriteria = session.createCriteria(databaseEntityName); if (criterion != null) { selectCriteria.add(criterion); } if (joins != null) { for (Map.Entry<String, Criterion> entry : joins.entrySet()) { String joinEntity = entry.getKey(); Criterion joinCriterion = entry.getValue(); selectCriteria.createCriteria(joinEntity).add(joinCriterion); } } selectCriteria.setProjection(properties).setFirstResult(from).setMaxResults(number) .setResultTransformer(new ViewInstanceTransformer(viewType, entityTypeName)); // Create the count criteria. Criteria countCriteria = session.createCriteria(databaseEntityName); if (criterion != null) { countCriteria.add(criterion); } if (joins != null) { for (Map.Entry<String, Criterion> entry : joins.entrySet()) { String joinEntity = entry.getKey(); Criterion joinCriterion = entry.getValue(); countCriteria.createCriteria(joinEntity).add(joinCriterion); } } countCriteria.setProjection(Projections.rowCount()); // Run a query to find out how many results there will be and update the list size. int count = (Integer) countCriteria.uniqueResult(); // Execute the query to get the block. List<ViewInstance> results = selectCriteria.list(); return new PagingResult(count, results); }
From source file:com.vaadin.data.hbnutil.HbnContainer.java
License:Open Source License
/** * Gets the ID's of all visible (after filtering and sorting) Items stored in the Container. The ID's cannot be * modified through the returned collection. If the container is Container.Ordered, the collection returned by this * method should follow that order. If the container is Container.Sortable, the items should be in the sorted order. * Calling this method for large lazy containers can be an expensive operation and should be avoided when practical. * /* w w w. ja va 2 s. c om*/ * Create an optimized query to return only identifiers. Note that this method does not scale well for large * database. At least Table is optimized so that it does not call this method. */ @Override public Collection<?> getItemIds() { logger.executionTrace(); Object key = HSess.checkInit(); // TODO: BUG: does not preserve sort order! final Criteria criteria = getCriteriaTL(); criteria.setProjection(Projections.id()); Collection<?> reto = criteria.list(); HSess.checkClose(key); return reto; }
From source file:com.vaadin.data.hbnutil.HbnContainer.java
License:Open Source License
/** * This is an internal HbnContainer utility method that gets the index of the given identifier. *///ww w .j a v a2 s . c o m private int slowIndexOfIdTL(Object entityId) { logger.executionTrace(); final Criteria criteria = getCriteriaTL().setProjection(Projections.id()); final List<?> list = criteria.list(); return list.indexOf(entityId); }
From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateSearchQueriesTest.java
License:Apache License
public void testIdQuery() { HibernateSearchQuery<Long> query = queries.getIds(BuildResult.class); assertEquals(0, query.count());//w ww . j a va 2 s. c o m buildResultDao.save(new BuildResult()); query.setProjection(Projections.id()); assertEquals(1, query.count()); }
From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleGenericDao.java
License:Apache License
@Override public List<PK> getAllIds() { DetachedCriteria criteria = DetachedCriteria.forClass(type); criteria.setProjection(Projections.id()); String idName = getHibernateTemplate().getSessionFactory().getClassMetadata(type) .getIdentifierPropertyName(); criteria.addOrder(Order.asc(idName)); return (List<PK>) getHibernateTemplate().findByCriteria(criteria); }
From source file:debop4k.data.orm.hibernate.dao.HibernateDao.java
License:Apache License
public int deleteAll(@NonNull Class<?> clazz, @NonNull Criteria criteria) { List ids = criteria.setProjection(Projections.id()).list(); for (Object id : ids) { deleteById(clazz, (Serializable) id); }//from w ww . j a v a2s . c om return ids.size(); }
From source file:edu.northwestern.bioinformatics.studycalendar.dao.SiteDao.java
License:BSD License
/** * Returns the IDs for all the sites visible according to the given parameters. * If the parameters indicate that all sites should be visible, it may return null. *//*from w w w . j a va 2 s . co m*/ @SuppressWarnings({ "unchecked" }) public Collection<Integer> getVisibleSiteIds(VisibleSiteParameters parameters) { if (parameters.isAllManagingSites() || parameters.isAllParticipatingSites()) { return null; // shortcut for all } else { Set<String> idents = new HashSet<String>(); idents.addAll(parameters.getParticipatingSiteIdentifiers()); idents.addAll(parameters.getManagingSiteIdentifiers()); if (idents.isEmpty()) { return Collections.emptySet(); } else { return getHibernateTemplate().findByCriteria(criteria() .add(MoreRestrictions.in("assignedIdentifier", idents)).setProjection(Projections.id())); } } }
From source file:edu.northwestern.bioinformatics.studycalendar.dao.StudyDao.java
License:BSD License
@SuppressWarnings({ "unchecked" }) private Collection<Integer> searchForVisibleIds(VisibleStudyParameters parameters, String search, boolean includeManaging, boolean includeParticipating, boolean includeSpecific) { if (log.isDebugEnabled()) { log.debug("Searching visible studies for {} with {}", parameters, search == null ? "no term" : "term \"" + search + '"'); if (!includeManaging) log.debug("- Excluding managing"); if (!includeParticipating) log.debug("- Excluding participating"); if (!includeSpecific) log.debug("- Excluding specific studies"); }//from w ww. j a v a2s .c o m List<DetachedCriteria> separateCriteria = new LinkedList<DetachedCriteria>(); if (parameters.isAllManagingSites() && includeManaging) { if (search == null) { return null; // shortcut for all } else { separateCriteria.add(criteria().add(searchRestriction(search))); } } else { // These are implemented as separate queries and then merged because // the criteria are too complex to reliably express in a single statement. if (includeSpecific && !parameters.getSpecificStudyIdentifiers().isEmpty()) { separateCriteria.add(criteria() .add(MoreRestrictions.in("assignedIdentifier", parameters.getSpecificStudyIdentifiers()))); } if (includeManaging && !parameters.getManagingSiteIdentifiers().isEmpty()) { separateCriteria.add(criteria().createAlias("managingSites", "ms", Criteria.LEFT_JOIN) .add(Restrictions.disjunction() .add(MoreRestrictions.in("ms.assignedIdentifier", parameters.getManagingSiteIdentifiers())) .add(Restrictions.isNull("ms.assignedIdentifier")) // <- unmanaged studies )); } if (includeParticipating) { if (parameters.isAllParticipatingSites()) { separateCriteria .add(criteria().createAlias("studySites", "ss").add(Restrictions.isNotNull("ss.id"))); } else if (!parameters.getParticipatingSiteIdentifiers().isEmpty()) { separateCriteria.add(criteria().createAlias("studySites", "ss").createAlias("ss.site", "s") .add(MoreRestrictions.in("s.assignedIdentifier", parameters.getParticipatingSiteIdentifiers()))); } } for (DetachedCriteria criteria : separateCriteria) { if (search != null) { criteria.add(searchRestriction(search)); } } } Set<Integer> ids = new LinkedHashSet<Integer>(); for (DetachedCriteria criteria : separateCriteria) { ids.addAll(getHibernateTemplate().findByCriteria(criteria.setProjection(Projections.id()))); } log.debug("Found IDs {}", ids); return ids; }