List of usage examples for org.hibernate.criterion Projections distinct
public static Projection distinct(Projection projection)
From source file:de.sub.goobi.forms.SearchForm.java
License:Open Source License
/** * Initialise drop down list of process property titles. */// w ww .j a va 2 s.c o m protected void initProcessPropertyTitles() { Session session = Helper.getHibernateSession(); Criteria crit = session.createCriteria(ProcessProperty.class); crit.addOrder(Order.asc("title")); crit.setProjection(Projections.distinct(Projections.property("title"))); this.processPropertyTitles.add(Helper.getTranslation("notSelected")); try { @SuppressWarnings("unchecked") List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list(); for (String itstr : results) { if (itstr != null) { this.processPropertyTitles.add(itstr); } } } catch (HibernateException hbe) { logger.warn("Catched HibernateException. List of process property titles could be empty!"); } }
From source file:de.sub.goobi.forms.SearchForm.java
License:Open Source License
/** * Initialise drop down list of step titles. *//* w w w . j a v a 2 s.com*/ protected void initStepTitles() { Session session = Helper.getHibernateSession(); Criteria crit = session.createCriteria(Task.class); crit.addOrder(Order.asc("title")); crit.setProjection(Projections.distinct(Projections.property("title"))); this.stepTitles.add(Helper.getTranslation("notSelected")); try { @SuppressWarnings("unchecked") List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list(); for (String result : results) { this.stepTitles.add(result); } } catch (HibernateException hbe) { logger.warn("Catched HibernateException. List of step titles could be empty!"); } }
From source file:de.sub.goobi.forms.SearchForm.java
License:Open Source License
/** * Initialise drop down list of template property titles. *///w w w . ja va 2 s. c o m protected void initTemplatePropertyTitles() { Session session = Helper.getHibernateSession(); Criteria crit = session.createCriteria(TemplateProperty.class); crit.addOrder(Order.asc("title")); crit.setProjection(Projections.distinct(Projections.property("title"))); this.templatePropertyTitles.add(Helper.getTranslation("notSelected")); try { @SuppressWarnings("unchecked") List<String> results = crit.setFirstResult(0).setMaxResults(Integer.MAX_VALUE).list(); for (String result : results) { this.templatePropertyTitles.add(result); } } catch (HibernateException hbe) { logger.warn("Catched HibernateException. List of template property titles could be empty!"); } }
From source file:debop4k.data.orm.hibernate.dao.HibernateDao.java
License:Apache License
private <P> Criteria buildProjectionCriteria(@NonNull Class<P> projectClass, @NonNull Criteria criteria, @NonNull Projection projections, boolean distinctResult) { if (distinctResult) criteria.setProjection(Projections.distinct(projections)); else//ww w .ja va2 s . c o m criteria.setProjection(projections); return criteria.setResultTransformer(Transformers.aliasToBean(projectClass)); }
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 w w w . j av a2s. co 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 www . j a v a2 s. 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:es.emergya.bbdd.dao.RolHome.java
License:Open Source License
@SuppressWarnings("unchecked") @Transactional(propagation = Propagation.REQUIRES_NEW, readOnly = true, rollbackFor = Throwable.class) public List<String> getAllString() { Session currentSession = getSession(); currentSession.clear();//w w w. jav a 2 s . c o m Criteria criteria = currentSession.createCriteria(Rol.class) .setProjection(Projections.distinct(Projections.property("nombre"))).addOrder(Order.asc("nombre")) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); return criteria.list(); }
From source file:es.emergya.bbdd.dao.RoutingHome.java
License:Open Source License
/** * Devuelve la lista de num calles que encajan con el patrn * /*from w w w .java2 s . c om*/ * @param pattern * @param num * @return list of streets */ @SuppressWarnings("unchecked") @Transactional(readOnly = true, rollbackFor = Throwable.class) public List<String> find(final String pattern, final Integer num) { List<String> lista = new ArrayList<String>(); try { lista = getSession().createCriteria(Routing.class) .setProjection(Projections.distinct(Projections.property("name"))).setMaxResults(num) .add(Restrictions.ilike("name", pattern)).addOrder(Order.asc("name")).list(); } catch (Exception e) { log.error("Error al buscar las calles :" + pattern, e); } return lista; }
From source file:es.itecban.deployment.environmentmanager.manager.DBCleanerImpl.java
License:Apache License
private String[] getEnvironmentsName() throws Exception { logger.fine("Getting the name of the existing environments in the database"); final Session session = this.sessionFactory.openSession(); session.beginTransaction();//from w ww.j a v a 2 s .c o m List<String> dttNameList; try { Criteria dttCriteria = session.createCriteria(DeploymentTargetType.class); dttCriteria.setProjection(Property.forName("name")); dttCriteria.setProjection(Projections.distinct(Property.forName("name"))); dttNameList = dttCriteria.list(); } catch (Exception e) { logger.severe("Error while retrieving the environments from the database " + e); throw new Exception("Error while retrieving the environments from the database " + e); } finally { session.close(); } return dttNameList.toArray(new String[dttNameList.size()]); }
From source file:es.itecban.deployment.environmentmanager.manager.EnvironmentManagerImpl.java
License:Apache License
/** * /*from w w w . jav a 2 s. co m*/ * @return * @throws Exception */ @Override public String[] getEnvironmentsName() throws Exception { logger.fine("Getting the name of the existing environments in the database"); final Session session = this.sessionFactory.openSession(); session.beginTransaction(); List<String> dttNameList; try { Criteria dttCriteria = session.createCriteria(DeploymentTargetType.class); dttCriteria.setProjection(Property.forName("name")); dttCriteria.setProjection(Projections.distinct(Property.forName("name"))); dttNameList = dttCriteria.list(); } catch (Exception e) { logger.severe("Error while retrieving the environments from the database " + e); throw new Exception("Error while retrieving the environments from the database " + e); } finally { session.close(); } return dttNameList.toArray(new String[dttNameList.size()]); }