List of usage examples for org.hibernate.criterion Projections rowCount
public static Projection rowCount()
From source file:com.eryansky.common.orm.core.hibernate.support.HibernateSupportDao.java
License:Apache License
/** * ?// w w w . j ava2s .c o m * * @return long */ public long entityCount(PropertyFilter... filters) { return (Long) createCriteria(Lists.newArrayList(filters)).setProjection(Projections.rowCount()) .uniqueResult(); }
From source file:com.eryansky.common.orm.core.hibernate.support.HibernateSupportDao.java
License:Apache License
/** * countCriteria./*from ww w . j a v a 2 s . co m*/ * * @param c Criteria * * @return long */ protected long countCriteriaResult(Criteria c) { CriteriaImpl impl = (CriteriaImpl) c; // Projection?ResultTransformer?OrderBy??,??Count? Projection projection = impl.getProjection(); ResultTransformer transformer = impl.getResultTransformer(); List<CriteriaImpl.OrderEntry> orderEntries = null; try { orderEntries = (List) ReflectionUtils.getFieldValue(impl, "orderEntries"); ReflectionUtils.setFieldValue(impl, "orderEntries", new ArrayList()); } catch (Exception e) { e.printStackTrace(); } // Count Long totalCountObject = (Long) c.setProjection(Projections.rowCount()).uniqueResult(); long totalCount = (totalCountObject != null) ? totalCountObject : 0; // ?Projection,ResultTransformerOrderBy?? c.setProjection(projection); if (projection == null) { c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY); } if (transformer != null) { c.setResultTransformer(transformer); } try { ReflectionUtils.setFieldValue(impl, "orderEntries", orderEntries); } catch (Exception e) { e.printStackTrace(); } return totalCount; }
From source file:com.eucalyptus.cloudwatch.common.internal.domain.alarms.AlarmManager.java
License:Open Source License
public static Long countMetricAlarms(String accountId) { try (final TransactionResource db = Entities.transactionFor(AlarmEntity.class)) { Criteria criteria = Entities.createCriteria(AlarmEntity.class); criteria = criteria.setProjection(Projections.rowCount()); if (accountId != null) { criteria = criteria.add(Restrictions.eq("accountId", accountId)); }/*from ww w . ja va2 s. c o m*/ return (Long) criteria.uniqueResult(); } }
From source file:com.eucalyptus.cloudwatch.domain.alarms.AlarmManager.java
License:Open Source License
public static Long countMetricAlarms(String accountId) { EntityTransaction db = Entities.get(AlarmEntity.class); try {/* ww w. j a v a2 s . c o m*/ Criteria criteria = Entities.createCriteria(AlarmEntity.class); criteria = criteria.setProjection(Projections.rowCount()); if (accountId != null) { criteria = criteria.add(Restrictions.eq("accountId", accountId)); } return (Long) criteria.uniqueResult(); } catch (RuntimeException ex) { Logs.extreme().error(ex, ex); throw ex; } finally { db.rollback(); } }
From source file:com.eucalyptus.entities.Entities.java
License:Open Source License
/** * Count the matching entities for the given example. * * @param example The example entity/*from w w w. ja va 2 s.c om*/ * @param criterion Additional restrictions for the query * @param aliases Any aliases necessary for the given criterion * @return The number of matching entities */ public static long count(final Object example, final Criterion criterion, final Map<String, String> aliases) { final Example qbe = Example.create(example); final Criteria criteria = getTransaction(example).getTxState().getSession() .createCriteria(example.getClass()).setReadOnly(true).setCacheable(false).add(qbe).add(criterion) .setProjection(Projections.rowCount()); for (final Map.Entry<String, String> aliasEntry : aliases.entrySet()) { criteria.createAlias(aliasEntry.getKey(), aliasEntry.getValue()); // inner join by default } final Number count = (Number) criteria.uniqueResult(); return count.longValue(); }
From source file:com.eucalyptus.objectstorage.metadata.DbObjectMetadataManagerImpl.java
License:Open Source License
@Override public long countValid(Bucket bucket) throws Exception { try (TransactionResource trans = Entities.transactionFor(ObjectEntity.class)) { /*Criteria queryCriteria = Entities.createCriteria(ObjectEntity.class); queryCriteria.add(Restrictions.eq("state", ObjectState.extant)) .createCriteria("bucket").add(Restrictions.eq("naturalId", bucket.getNaturalId()))*/ Criteria queryCriteria = Entities.createCriteria(ObjectEntity.class) .add(Restrictions.eq("state", ObjectState.extant)).setProjection(Projections.rowCount()); queryCriteria = getSearchByBucket(queryCriteria, bucket); queryCriteria.setReadOnly(true); final Number count = (Number) queryCriteria.uniqueResult(); trans.commit();/*from w ww . j ava2 s . c o m*/ return count.longValue(); } catch (Throwable e) { LOG.error("Error getting object count for bucket " + bucket.getBucketName(), e); throw new Exception(e); } }
From source file:com.eucalyptus.objectstorage.WalrusManager.java
License:Open Source License
private boolean bucketHasSnapshots(String bucketName) throws Exception { EntityWrapper<WalrusSnapshotInfo> dbSnap = null; try {/*from w ww. j a va 2 s . c o m*/ dbSnap = EntityWrapper.get(WalrusSnapshotInfo.class); WalrusSnapshotInfo walrusSnapInfo = new WalrusSnapshotInfo(); walrusSnapInfo.setSnapshotBucket(bucketName); Criteria snapCount = dbSnap.createCriteria(WalrusSnapshotInfo.class).add(Example.create(walrusSnapInfo)) .setProjection(Projections.rowCount()); snapCount.setReadOnly(true); Long rowCount = (Long) snapCount.uniqueResult(); dbSnap.rollback(); if (rowCount != null && rowCount.longValue() > 0) { return true; } return false; } catch (Exception e) { if (dbSnap != null) { dbSnap.rollback(); } throw e; } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void countObjectOrderByName() throws Exception { Session session = open();//from w w w. j a v a 2s . com try { Criteria main = session.createCriteria(RUser.class, "u"); main.add(Restrictions.and(Restrictions.eq("u.name.orig", "cpt. Jack Sparrow"), Restrictions.eq("u.name.norm", "cpt jack sparrow"))); main.setProjection(Projections.rowCount()); String expected = HibernateToSqlTranslator.toSql(main); EqualFilter filter = EqualFilter.createEqual(UserType.F_NAME, UserType.class, prismContext, null, new PolyString("cpt. Jack Sparrow", "cpt jack sparrow")); ObjectQuery query = ObjectQuery.createObjectQuery(filter); query.setPaging(ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING)); String real = getInterpretedQuery(session, UserType.class, query, true); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
@Test public void countObjectOrderByNameWithoutFilter() throws Exception { Session session = open();/* w w w . j ava 2s . co m*/ try { Criteria main = session.createCriteria(RObject.class, "o"); main.setProjection(Projections.rowCount()); String expected = HibernateToSqlTranslator.toSql(main); ObjectPaging paging = ObjectPaging.createPaging(null, null, ObjectType.F_NAME, OrderDirection.ASCENDING); ObjectQuery query = ObjectQuery.createObjectQuery(null, paging); String real = getInterpretedQuery(session, ObjectType.class, query, true); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }
From source file:com.evolveum.midpoint.repo.sql.QueryInterpreterTest.java
License:Apache License
/** * Q{AND: (EQUALS: parent, PPV(null)),PAGING: O: 0,M: 5,BY: name, D:ASCENDING, * * @throws Exception/*from w w w.j a v a2 s. c o m*/ */ @Test public void countTaskOrderByName() throws Exception { Session session = open(); try { Criteria main = session.createCriteria(RTask.class, "t"); main.add(Restrictions.isNull("t.parent")); main.setProjection(Projections.rowCount()); String expected = HibernateToSqlTranslator.toSql(main); EqualFilter filter = EqualFilter.createEqual(TaskType.F_PARENT, TaskType.class, prismContext, null); ObjectQuery query = ObjectQuery.createObjectQuery(filter); query.setPaging(ObjectPaging.createPaging(null, null, TaskType.F_NAME, OrderDirection.ASCENDING)); String real = getInterpretedQuery(session, TaskType.class, query, true); LOGGER.info("exp. query>\n{}\nreal query>\n{}", new Object[] { expected, real }); AssertJUnit.assertEquals(expected, real); } finally { close(session); } }