@Override public List<Customer> findAll(int page, int pageSize) { TypedQuery<Customer> query = em.createQuery("select c from Customer c", Customer.class); query.setFirstResult(page * pageSize); query.setMaxResults(pageSize); return query.getResultList(); }
@Override public List<Customer> findByLastname(String lastname, int page, int pageSize) { TypedQuery<Customer> query = em.createQuery("select c from Customer c where c.lastname = ?1", Customer.class); query.setParameter(1, lastname); query.setFirstResult(page * pageSize); query.setMaxResults(pageSize); return query.getResultList(); } }
protected List<Sku> readAllActiveSkusInternal(Integer pageSize, Date currentDate, Long lastId) { CriteriaQuery<Sku> criteria = getCriteriaForActiveSkus(currentDate, lastId); TypedQuery<Sku> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.setMaxResults(pageSize).getResultList(); }
@Override public <S> Entry<S> loadSaga(Class<S> sagaType, String sagaIdentifier) { EntityManager entityManager = entityManagerProvider.getEntityManager(); final Class<? extends SimpleSerializedObject<?>> serializedObjectType = serializedObjectType(); List<? extends SimpleSerializedObject<?>> serializedSagaList = entityManager.createNamedQuery(LOAD_SAGA_NAMED_QUERY, serializedObjectType) .setParameter("sagaId", sagaIdentifier) .setMaxResults(1) .getResultList(); if (serializedSagaList == null || serializedSagaList.isEmpty()) { return null; } final SimpleSerializedObject<?> serializedSaga = serializedSagaList.get(0); S loadedSaga = serializer.deserialize(serializedSaga); Set<AssociationValue> associationValues = loadAssociationValues(entityManager, sagaType, sagaIdentifier); if (logger.isDebugEnabled()) { logger.debug("Loaded saga id [{}] of type [{}]", sagaIdentifier, serializedSaga.getType().getName()); } return new EntryImpl<>(associationValues, loadedSaga); }
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW) boolean isExist(Object id) { String sql = String.format("select count(*) from %s ref where ref.%s = :id", voClass.getSimpleName(), voPrimaryKeyField.getName()); TypedQuery<Long> q = getEntityManager().createQuery(sql, Long.class); q.setParameter("id", id); q.setMaxResults(1); Long count = q.getSingleResult(); return count > 0; }
@Override public List<Product> readProductsByName(@Nonnull String searchName, @Nonnull int limit, @Nonnull int offset) { TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_NAME", Product.class); query.setParameter("name", searchName + '%'); query.setFirstResult(offset); query.setMaxResults(limit); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getResultList(); }
@Override public List<Product> readAllProducts(int limit, int offset) { TypedQuery<Product> query = em.createNamedQuery("BC_READ_ALL_PRODUCTS", Product.class); //don't cache - could take up too much memory query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); }
@Test public void testPaginationQuery() { // prepare some test data doInJPA( this::entityManagerFactory, entityManager -> { for ( int i = 1; i <= 20; ++i ) { final SimpleEntity entity = new SimpleEntity( i, "Entity" + i ); entityManager.persist( entity ); } } ); // This would fail with "index 2 out of range" within TopLimitHandler // The fix addresses this problem which only occurs when using SQLServerDialect explicitly. doInJPA( this::entityManagerFactory, entityManager -> { List<SimpleEntity> results = entityManager .createQuery( "SELECT o FROM SimpleEntity o WHERE o.id >= :firstId ORDER BY o.id", SimpleEntity.class ) .setParameter( "firstId", 10 ) .setMaxResults( 5 ) .getResultList(); // verify that the paginated query returned the right ids. final List<Integer> ids = results.stream().map( SimpleEntity::getId ).collect( Collectors.toList() ); assertEquals( Arrays.asList( 10, 11, 12, 13, 14 ), ids ); } ); }
protected List<Product> readAllActiveProductsInternal(Integer pageSize, Date currentDate, Long lastId) { CriteriaQuery<Product> criteria = getCriteriaForActiveProducts(currentDate, lastId); TypedQuery<Product> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.setMaxResults(pageSize).getResultList(); }
private ErrorCode refuseLiveMigrationForLocalStorage(VmInstanceInventory vm) { List<String> volUuids = CollectionUtils.transformToList(vm.getAllVolumes(), VolumeInventory::getUuid); String sql = "select count(ps)" + " from PrimaryStorageVO ps, VolumeVO vol" + " where ps.uuid = vol.primaryStorageUuid" + " and vol.uuid in (:volUuids)" + " and ps.type = :ptype"; TypedQuery<Long> q = dbf.getEntityManager().createQuery(sql, Long.class); q.setParameter("volUuids", volUuids); q.setParameter("ptype", LocalStorageConstants.LOCAL_STORAGE_TYPE); q.setMaxResults(1); Long count = q.getSingleResult(); if (count > 0) { return operr("unable to live migrate with local storage. The vm[uuid:%s] has volumes on local storage," + "to protect your data, please stop the vm and do the volume migration", vm.getUuid()); } return null; }
@Override public List<Category> readCategoriesByName(String categoryName, int limit, int offset) { TypedQuery<Category> query = em.createNamedQuery("BC_READ_CATEGORY_BY_NAME", Category.class); query.setParameter("categoryName", categoryName); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); }
@Override public List<Sku> readAllSkus(int offset, int limit) { TypedQuery<Sku> query = em.createNamedQuery("BC_READ_ALL_SKUS", Sku.class); //don't cache - could take up too much memory query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); }
"WHERE e.globalIndex > :token OR e.globalIndex IN :gaps ORDER BY e.globalIndex ASC", Object[].class ).setParameter("gaps", previousToken.getGaps()); return query.setParameter("token", previousToken == null ? -1L : previousToken.getIndex()) .setMaxResults(batchSize) .getResultList(); }); List<TrackedEventData<?>> result = new ArrayList<>();
"select DISTINCT p from Person p", Person.class) .setLockMode( LockModeType.PESSIMISTIC_WRITE ) .getResultList(); .getResultList(); .setMaxResults( 10 ) .unwrap( Query.class ) .setLockOptions(
sq.setParameter("uuid", spec.getImageUuid()); long imageSize = sq.getSingleResult(); sql = "select count(i) from ImageCacheVO i where i.installUrl like :mark and i.primaryStorageUuid in (:psUuids)"; TypedQuery<Long> iq = dbf.getEntityManager().createQuery(sql, Long.class); iq.setParameter("psUuids", ref.getPrimaryStorageUuid()); iq.setParameter("mark", String.format("%%hostUuid://%s%%", ref.getHostUuid())); iq.setMaxResults(1); long count = iq.getSingleResult();
public List<Product> readActiveProductsByCategoryInternal(Long categoryId, Date currentDate, int limit, int offset) { TypedQuery<Product> query = em.createNamedQuery("BC_READ_ACTIVE_PRODUCTS_BY_CATEGORY", Product.class); query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId)); query.setParameter("currentDate", currentDate); query.setFirstResult(offset); query.setMaxResults(limit); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getResultList(); }
@Override public List<Order> findCarts(String[] names, OrderStatus[] statuses, Date dateCreatedMinThreshold, Boolean isPreview, int startPos, int length, List<Long> excludedIds) { TypedQuery<Order> query = buildCartQuery(names, statuses, dateCreatedMinThreshold, isPreview, Order.class, excludedIds); query.setFirstResult(startPos); query.setMaxResults(length); return query.getResultList(); }
@Transactional(readOnly = true) private VirtualRouterVmInventory findRunningVirtualRouterForRule(String ruleUuid) { String sql = "select vr from VirtualRouterPortForwardingRuleRefVO ref, VirtualRouterVmVO vr where ref.virtualRouterVmUuid = vr.uuid and ref.uuid = :ruleUuid and vr.state = :vrState"; TypedQuery<VirtualRouterVmVO> q = dbf.getEntityManager().createQuery(sql, VirtualRouterVmVO.class); q.setParameter("ruleUuid", ruleUuid); q.setParameter("vrState", VmInstanceState.Running); q.setMaxResults(1); List<VirtualRouterVmVO> vrs = q.getResultList(); if (vrs.isEmpty()) { return null; } else { return VirtualRouterVmInventory.valueOf(vrs.get(0)); } }
cq.orderBy(builder.asc(from.get("mySystem")), builder.asc(from.get("myCode"))); TypedQuery<TagDefinition> q = myEntityManager.createQuery(cq); q.setMaxResults(getConfig().getHardTagListLimit()); for (TagDefinition next : q.getResultList()) { retVal.add(next.toTag());
" and ps.type = :type"; TypedQuery<Long> q = dbf.getEntityManager().createQuery(sql, Long.class); q.setParameter("uuid", vm.getRootVolume().getPrimaryStorageUuid()); q.setParameter("type", LocalStorageConstants.LOCAL_STORAGE_TYPE); q.setMaxResults(1); Long count = q.getSingleResult(); if (count == 0) {