掲示板

how to use dynamicQueryCount

thumbnail
11年前 に Alain Dresse によって更新されました。

how to use dynamicQueryCount

Junior Member 投稿: 95 参加年月日: 11/07/18 最新の投稿
Hi,

I am using dynamic queries for a search container, and have an issue extracting both the count and the results from the same dynamic query.

I do the following:

Create the dynamic query, and set the conditions

	DynamicQuery dynamicQuery = MBThreadLocalServiceUtil.dynamicQuery();
	Criterion criterion = RestrictionsFactoryUtil.in("categoryId", categoryIds);
	dynamicQuery.add(criterion);


If I then do

	total="<%= MBThreadLocalServiceUtil.dynamicQueryCount(dynamicQuery) %>"
	results="<%= MBThreadLocalServiceUtil.dynamicQuery(
		dynamicQuery, searchContainer.getStart(), searchContainer.getEnd()) %>"


I get an error on the second line, becaus Long cannot be converted to MBThread.

The only workaround I have found is to create two dynamicQueries, one for the count and one for the entries. Is there a way to reuse the same dynamic query for both ? Wouldn't it make sense to have dynamicQueryCount not modify the dynamicQuery it receives (cloning it before setting the projection ? don't know if this is possible...)

Best regards,
Alain
thumbnail
10年前 に Punam Shah によって更新されました。

RE: how to use dynamicQueryCount

Regular Member 投稿: 117 参加年月日: 12/01/20 最新の投稿
Please try this :

  total="<%= (int)MBThreadLocalServiceUtil.dynamicQueryCount(dynamicQuery) %>"
results="<%= MBThreadLocalServiceUtil.dynamicQuery(
    dynamicQuery, searchContainer.getStart(), searchContainer.getEnd()) %>"


Hope, this will help you, instead of creating two queries.
thumbnail
10年前 に Alain Dresse によって更新されました。

RE: how to use dynamicQueryCount

Junior Member 投稿: 95 参加年月日: 11/07/18 最新の投稿
Thanks for the answer,

Unfortunately, I have reviewed my code since, moving the queries to custom sql, so it is no longer that easy to test the changes you suggest...

Best regards,
Alain
8年前 に Tamas Toth によって更新されました。

RE: how to use dynamicQueryCount

New Member 投稿: 10 参加年月日: 13/07/23 最新の投稿
Hi,

I know it is an old thread, but I am facing similar problems, so it may worth to add here.

I tried to create a DynamicQuery based search container, using the following code. (LoginLog is a custom entity which I use to collect some user statistics, and I want to filter the records for a time range, ordered by the timestamp.)

Criterion filter = RestrictionsFactoryUtil.between("timestamp", dateFrom, dateTo);
DynamicQuery query = DynamicQueryFactoryUtil.forClass(LoginLog.class).add(filter);
query.addOrder(OrderFactoryUtil.asc("timestamp"));

logs = LoginLogLocalServiceUtil.dynamicQuery(query, searchContainer.getStart(), searchContainer.getEnd());
total = (int)LoginLogLocalServiceUtil.dynamicQueryCount(query2);


I had two issues:
1. Without the ordering, the first page was working fine, but if I navigated to other pages, the total became 0 (so dynamicQueryCount returned this value).
2. After adding the order clause, I got an exception saying "invalid ORDER BY expression". I figured out that it was thrown by the dynamicQueryCount method.

When I created a second query with the filter but without the order, and called dynamicQueryCount on this secind one, it returned the correct result, so my search container is working fine. But I don't think it is the most effective way of solving it.
thumbnail
10年前 に G R Rajesh Babu によって更新されました。

RE: how to use dynamicQueryCount

Regular Member 投稿: 161 参加年月日: 10/02/08 最新の投稿
Hi Alain,

Please let us know whether the total="<%= (int)MBThreadLocalServiceUtil.dynamicQueryCount(dynamicQuery) %>" is working fine or not.