Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Problem with Dynamic Query
I am having an issue with Dynamic Query. It seems like the Dynamic query does not work with BlogEntry, but it works in other cases for example with user.
Code:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BlogEntry.class);
Criterion criterionEntry = RestrictionsFactoryUtil.eq("groupId",siteId);dynamicQuery.add(criterionEntry);
List<BlogsEntry> testBlogEntries = BlogsEntryLocalServiceUtil.dynamicQuery(dynamicQuery);
Note:- I have tried working around with asset but then with assetEntry i am unable to get the status .
Code:
DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BlogEntry.class);
Criterion criterionEntry = RestrictionsFactoryUtil.eq("groupId",siteId);dynamicQuery.add(criterionEntry);
List<BlogsEntry> testBlogEntries = BlogsEntryLocalServiceUtil.dynamicQuery(dynamicQuery);
Note:- I have tried working around with asset but then with assetEntry i am unable to get the status .
Hi,
Are you getting error or fetching empty result set. In case your getting error please share the error?
Are you getting error or fetching empty result set. In case your getting error please share the error?
I am getting an empty list, which is wrong sinceI already have data in the database.
I think the error is because of the import that is used for blogEntry class. "import com.liferay.blogs.kernel.model.BlogsEntry" and if i look into the classname_ table then for BlogEntry Id they mention the value as "com.liferay.blogs.model.BlogsEntry"
but i am not sure since i cannot pass this option "com.liferay.blogs.model.BlogsEntry" to the Dynamic Query
I think the error is because of the import that is used for blogEntry class. "import com.liferay.blogs.kernel.model.BlogsEntry" and if i look into the classname_ table then for BlogEntry Id they mention the value as "com.liferay.blogs.model.BlogsEntry"
but i am not sure since i cannot pass this option "com.liferay.blogs.model.BlogsEntry" to the Dynamic Query
Try replacing DynamicQueryFactoryUtil.forClass({className}.class,cl) with
In case of BlogsEntry, replace DynamicQueryFactoryUtil.forClass(BlogEntry.class, ....) with:
More info see:
{className}LocalServiceUtil.dynamicQuery()That will avoid classloader issuesIn case of BlogsEntry, replace DynamicQueryFactoryUtil.forClass(BlogEntry.class, ....) with:
BlogsEntryLocalServiceUtil.dynamicQuery()More info see:
Thanks Jorge Díaz It worked.I have one following question.Now i am able to get the list by adding the dynamic query which look something like this
How can i iterate this list. If I use com.liferay.blogs.kernel.model.BlogsEntry, to iterate, then I get the following error
DynamicQuery dynamicQuery = BlogsEntryLocalServiceUtil.dynamicQuery();
Criterion criterionEntry = RestrictionsFactoryUtil.eq("groupId",themeDisplay.getScopeGroupId());
List<BlogsEntry> blogEntryList = BlogsEntryLocalServiceUtil.dynamicQuery(dynamicQuery, page * count, (page + 1) * count);
for (BlogsEntry blogEntryObject : blogEntryList) {
blogEntryObject.getTitle();
}
How can i iterate this list. If I use com.liferay.blogs.kernel.model.BlogsEntry, to iterate, then I get the following error
java.lang.ClassCastException: com.liferay.blogs.model.impl.BlogsEntryImpl cannot be cast to com.liferay.blogs.kernel.model.BlogsEntry
I solved the issue by adding the dependencies and changing the import for BlogEntry
changed the imports to
compileOnly group: 'com.liferay', name: 'com.liferay.blogs.api', version: '4.0.0'
changed the imports to
import com.liferay.blogs.model.BlogsEntry;
import com.liferay.blogs.service.BlogsEntryLocalServiceUtil;