Problem with Dynamic Query

Niharika Singhal, modified 6 Years ago. New Member Posts: 4 Join Date: 9/13/19 Recent Posts
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 . 
thumbnail
Mohammed Yasin, modified 6 Years ago. Liferay Master Posts: 593 Join Date: 8/8/14 Recent Posts
Hi,
Are you getting error or fetching empty result set. In case your getting error please share the error?
Niharika Singhal, modified 6 Years ago. New Member Posts: 4 Join Date: 9/13/19 Recent Posts
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
thumbnail
Jorge Díaz, modified 6 Years ago. Liferay Master Posts: 753 Join Date: 1/9/14 Recent Posts
Try replacing DynamicQueryFactoryUtil.forClass({className}.class,cl) with
{className}LocalServiceUtil.dynamicQuery()
That will avoid classloader issues

In case of BlogsEntry, replace DynamicQueryFactoryUtil.forClass(BlogEntry.class, ....) with:
BlogsEntryLocalServiceUtil.dynamicQuery()

More info see:
Niharika Singhal, modified 6 Years ago. New Member Posts: 4 Join Date: 9/13/19 Recent Posts
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

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 
Niharika Singhal, modified 6 Years ago. New Member Posts: 4 Join Date: 9/13/19 Recent Posts
I solved the issue by adding the dependencies and changing  the import for BlogEntry 
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;