List of usage examples for org.hibernate.criterion Projections groupProperty
public static PropertyProjection groupProperty(String propertyName)
From source file:ca.myewb.controllers.mailing.ListMember.java
License:Open Source License
private List<GroupModel> getControllableGroups() { List<GroupModel> lists; if (currentUser.isAdmin()) { lists = (new SafeHibList<GroupModel>(hibernateSession .createQuery("SELECT g FROM GroupModel g where g.visible=true and g.admin=false"))).list(); } else {//ww w . j a va 2s.c o m Criteria crit = hibernateSession.createCriteria(GroupModel.class); crit.createAlias("roles", "r"); crit.add(Restrictions.eq("r.user", currentUser)); crit.add(Restrictions.isNull("r.end")); crit.add(Restrictions.eq("r.level", new Character('l'))); crit.add(Restrictions.eq("visible", new Boolean(true))); crit.add(Restrictions.eq("admin", new Boolean(false))); crit.setProjection(Projections.groupProperty("id")); lists = (new SafeHibList<GroupModel>(crit)).list(); Iterator it = lists.iterator(); List<GroupModel> lists2 = new ArrayList<GroupModel>(); while (it.hasNext()) { lists2.add((GroupModel) hibernateSession.get(GroupModel.class, (Integer) it.next())); } lists = lists2; lists.addAll(currentUser.getChapter().getVisibleChildren()); } return lists; }
From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java
License:Open Source License
@SuppressWarnings("unchecked") static public List<Object[]> findMaxObject() throws HibernateException { Session s = null;//from w w w. j a v a 2 s . c om try { s = openSession(); //Query q = s.createQuery("select count(ae.subject), ae.subject FROM "+ AE_CLASS + " AS ae "+ //"GROUP BY ae.subject ORDER BY count(ae.subject) desc"); Criteria crit = s.createCriteria(AuditEvent.class); crit.setProjection(Projections.projectionList().add(Projections.count("object").as("gcount")) .add(Projections.groupProperty("object"))); crit.addOrder(Order.desc("gcount")); crit.setMaxResults(10); return crit.list(); } finally { close(s); } }
From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.EntityPropertyTypeDAO.java
License:Apache License
public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) { assert termsWithStats != null : "Unspecified terms."; assert vocabulary != null : "Unspecified vocabulary."; assert termsWithStats.size() == vocabulary.getTerms() .size() : "Sizes of terms to be filled and vocabulary terms don't match."; Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>( termsWithStats.size());/*from w w w . j a v a 2 s . c o m*/ for (VocabularyTermWithStats termWithStats : termsWithStats) { Long id = termWithStats.getTerm().getId(); termsById.put(id, termWithStats); } final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass()); // alias is the easiest way to restrict on association using criteria criteria.createAlias("vocabularyTerm", "term"); criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary)); ProjectionList projectionList = Projections.projectionList(); projectionList.add(Projections.rowCount()); projectionList.add(Projections.groupProperty("term.id")); criteria.setProjection(projectionList); final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria)); for (Object[] result : results) { Integer numberOfUsages = (Integer) result[0]; Long termId = (Long) result[1]; termsById.get(termId).registerUsage(entityKind, numberOfUsages); } }
From source file:co.com.siscomputo.administracion.logic.UsuarioLogic.java
/** * * @param idGrupo/*from w w w . jav a 2 s . c o m*/ * @return */ public ObjetoRetornaEntity listaUsuariosPorGrupo(int idGrupo) { ObjetoRetornaEntity retorna = new ObjetoRetornaEntity(); try { String validaConexion = initOperation(); if (!"Ok".equalsIgnoreCase(validaConexion)) { } else { //System.out.println("id GRupo: "+idGrupo); Criteria criteria = sesion.createCriteria(UsuarioGrupoUsuarioEntity.class) .add(Restrictions.eq("grupoUsuario.idGrupoUsuarios", idGrupo)); criteria.setProjection(Projections.groupProperty("usuario")); retorna.setRetorna((ArrayList<Object>) criteria.list()); retorna.setNumeroRespuesta(100); retorna.setTrazaRespuesta("Carga de usuario Por grupo"); } } catch (Exception e) { retorna.setNumeroRespuesta(3); retorna.setTrazaRespuesta("ERROR: " + e); e.printStackTrace(); } finally { try { //sesion.close(); sesion.close(); } catch (HibernateException hibernateException) { hibernateException.printStackTrace(); } } return retorna; }
From source file:com.amalto.core.storage.hibernate.StandardQueryHandler.java
License:Open Source License
private ProjectionList optimizeProjectionList(ComplexTypeMetadata mainType, ProjectionList oldProjectionList) { ProjectionList newProjectionList = null; RDBMSDataSource dataSource = (RDBMSDataSource) storage.getDataSource(); if (dataSource.getDialectName() != RDBMSDataSource.DataSourceDialect.ORACLE_10G) { newProjectionList = oldProjectionList; for (FieldMetadata keyField : mainType.getKeyFields()) { newProjectionList.add(Projections.groupProperty(keyField.getName())); }/* w ww. ja v a 2 s . c o m*/ } else { // ORACLE need to GROUP BY all selected fields newProjectionList = Projections.projectionList(); Set<String> groupBys = new LinkedHashSet<String>();// GROUP BY fields ProjectionList extraProjectionList = Projections.projectionList(); for (int i = 0; i < oldProjectionList.getLength(); i++) { String propertyName = null; Projection oldProjection = oldProjectionList.getProjection(i); if (oldProjection instanceof SQLProjection) { // Group Size newProjectionList.add(oldProjection); } else if (oldProjection instanceof PropertyProjection) {// normal fields propertyName = ((PropertyProjection) oldProjection).getPropertyName(); newProjectionList.add(Projections.groupProperty(propertyName)); } else if (oldProjection instanceof AggregateProjection) {// Max, Min propertyName = ((AggregateProjection) oldProjection).getPropertyName(); newProjectionList.add(oldProjection); extraProjectionList.add(Projections.groupProperty(propertyName)); } if (propertyName != null) { groupBys.add(propertyName); } } // Add key fields to GROUP BY for (FieldMetadata keyField : mainType.getKeyFields()) { String keyFieldName = mainType.getName() + '.' + keyField.getName(); if (!groupBys.contains(keyFieldName)) { extraProjectionList.add(Projections.groupProperty(keyFieldName)); } } for (int i = 0; i < extraProjectionList.getLength(); i++) { newProjectionList.add(extraProjectionList.getProjection(i)); } } return newProjectionList; }
From source file:com.amalto.core.storage.hibernate.StandardQueryHandler.java
License:Open Source License
@Override public StorageResults visit(OrderBy orderBy) { TypedExpression orderByExpression = orderBy.getExpression(); CriterionFieldCondition fieldCondition = new CriterionFieldCondition(); FieldCondition condition = orderByExpression.accept(fieldCondition); if (orderByExpression instanceof Field) { Field field = (Field) orderByExpression; FieldMetadata userFieldMetadata = field.getFieldMetadata(); ComplexTypeMetadata containingType = getContainingType(userFieldMetadata); Set<String> aliases = getAliases(containingType, field); condition.criterionFieldNames = new ArrayList<>(aliases.size()); for (String alias : aliases) { addCondition(condition, alias, userFieldMetadata); }//from www . j a v a 2 s.c o m } if (orderByExpression instanceof Count) { Count count = (Count) orderByExpression; String propertyName = count.getExpression().accept(fieldCondition).criterionFieldNames.get(0); ProjectionList list = projectionList; if (projectionList instanceof ReadOnlyProjectionList) { list = ((ReadOnlyProjectionList) projectionList).inner(); } list.add(Projections.groupProperty(propertyName)); String alias = "x_talend_countField" + countAggregateIndex++; //$NON-NLS-1$ list.add(Projections.count(propertyName).as(alias)); switch (orderBy.getDirection()) { case ASC: criteria.addOrder(Order.asc(alias)); break; case DESC: criteria.addOrder(Order.desc(alias)); break; } } if (condition != null) { for (String fieldName : condition.criterionFieldNames) { OrderBy.Direction direction = orderBy.getDirection(); switch (direction) { case ASC: criteria.addOrder(Order.asc(fieldName)); break; case DESC: criteria.addOrder(Order.desc(fieldName)); break; } } } return null; }
From source file:com.aquest.emailmarketing.web.dao.TrackingResponseDao.java
/** * Gets the no of opens by broadcast.// ww w.j av a 2 s . c o m * * @param broadcast_id the broadcast_id * @return the no of opens by broadcast */ public int getNoOfOpensByBroadcast(String broadcast_id) { Criteria crit = session().createCriteria(TrackingResponse.class); crit.add(Restrictions.eq("broadcast_id", broadcast_id)); crit.add(Restrictions.eq("response_type", "Open")); List result = crit.setProjection(Projections.projectionList().add(Projections.groupProperty("unique_id"))) .list(); return result.size(); }
From source file:com.aquest.emailmarketing.web.dao.TrackingResponseDao.java
/** * Gets the no of click by broadcast.//from w w w. ja v a 2 s . com * * @param broadcast_id the broadcast_id * @return the no of click by broadcast */ public int getNoOfClickByBroadcast(String broadcast_id) { Criteria crit = session().createCriteria(TrackingResponse.class); crit.add(Restrictions.eq("broadcast_id", broadcast_id)); crit.add(Restrictions.eq("response_type", "Click")); List result = crit.setProjection(Projections.projectionList().add(Projections.groupProperty("unique_id"))) .list(); return result.size(); }
From source file:com.ateam.hibernate.HibernateDAOImpl.java
public String checkRole(String strUserName, String password) throws DataAccessException, java.sql.SQLException { String obj = null;/*from www .ja v a 2 s. c om*/ DetachedCriteria critthree = DetachedCriteria.forClass(UserAttr.class); ProjectionList pl = Projections.projectionList(); pl.add(Projections.groupProperty("userRole")); critthree.add(Restrictions.eq("userName", strUserName)); critthree.add(Restrictions.eq("userPassword", password)); critthree.setProjection(pl); List objs = getHibernateTemplate().findByCriteria(critthree); if ((objs != null) && (objs.size() > 0)) { obj = (String) objs.get(0); } return obj; }
From source file:com.ateam.hibernate.HibernateDAOImpl.java
public String checkFullName(String strUserName, String password) throws DataAccessException, java.sql.SQLException { String obj = null;//from w ww. j a v a2 s . c om DetachedCriteria critname = DetachedCriteria.forClass(UserAttr.class); ProjectionList pl = Projections.projectionList(); pl.add(Projections.groupProperty("userFullName")); critname.add(Restrictions.eq("userName", strUserName)); critname.add(Restrictions.eq("userPassword", password)); critname.setProjection(pl); List objs = getHibernateTemplate().findByCriteria(critname); if ((objs != null) && (objs.size() > 0)) { obj = (String) objs.get(0); } return obj; }