Message Boards

Liferay 7.2 - Query JournalArticles by categoryId with Hits

Fabio Carvalho, modified 4 Years ago.

Liferay 7.2 - Query JournalArticles by categoryId with Hits

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

I am doing my queries to JournalArticle using Hits:
Hits hits = JournalArticleLocalServiceUtil.search(
    themeDisplay.getCompanyId(),
    group.getGroupId(),
    Arrays.asList(JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID),
    JournalArticleConstants.CLASSNAME_ID_DEFAULT, 
    "MY_STRUCTURE_KEY",
    null, 
    null, 
    null, 
    0, 
    MAX_ITEMS, 
    SortFactoryUtil.create(Field.MODIFIED_DATE, Sort.DOUBLE_TYPE, true)
);

But now I want to query the JournalArticles with a specific category. The "search" method has the "java.util.LinkedHashMap<String, Object> params" param. Could I use it to query for categoryId? What would be the correct way to get JournalArticles from a specific category?
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Liferay 7.2 - Query JournalArticles by categoryId with Hits

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
So categories and tags are actually part of the asset, not the journal article.

You should be able to find an appropriate search() method on AssetEntryLocalService to incorporate category into your query. Once you have the assets, you'll have the articles.
Fabio Carvalho, modified 4 Years ago.

RE: Liferay 7.2 - Query JournalArticles by categoryId with Hits

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi David,

Thanks for your answer! I understood what you have said and the AssetEntryLocalServiceUtil has indeed a search method with the category param. The problem its that all the search methods that has the category param, don't have a sort param... Let's suppose that I want to find only the last 5 AssetEntries that were modified from category X. How can I do this? Getting all the AssetEntries from the category X and then sort with Java?

I am afraid this aproach will not be that efficient.
thumbnail
David H Nebinger, modified 4 Years ago.

RE: Liferay 7.2 - Query JournalArticles by categoryId with Hits

Liferay Legend Posts: 14919 Join Date: 9/2/06 Recent Posts
you might want to check that, modified date is likely updated on both the asset and the article.

You're right about the no sort option; you may need to go to a DQ in order to get that in there.
Fabio Carvalho, modified 4 Years ago.

RE: Liferay 7.2 - Query JournalArticles by categoryId with Hits

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
I have been testing a DynamicQuery to do this:

DynamicQuery categoryQuery = AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery();
categoryQuery.add(PropertyFactoryUtil.forName(Field.ASSET_CATEGORY_ID).eq(Long.parseLong(categoryId)));
categoryQuery.setProjection(ProjectionFactoryUtil.property("assetEntryId"));
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
DynamicQuery assetQuery = AssetEntryLocalServiceUtil.dynamicQuery();
assetQuery.add(PropertyFactoryUtil.forName(Field.CLASS_NAME_ID).eq(PortalUtil.getClassNameId(JournalArticle.class.getName())));
assetQuery.add(PropertyFactoryUtil.forName("entryId").in(categoryQuery));
assetQuery.addOrder(OrderFactoryUtil.desc("modifiedDate"));

List<assetentry> assets = AssetEntryLocalServiceUtil.dynamicQuery(assetQuery);
System.out.println(assets.size());</assetentry>

But this is throwing an exception:

2020-02-03 12:15:50.049 ERROR [http-nio-8080-exec-10][BasePersistenceImpl:436] Caught unexpected exception
org.hibernate.MappingException: Unknown entity: com.liferay.asset.entry.rel.model.impl.AssetEntryAssetCategoryRelImpl
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
    at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:66)
    at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
    at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
    at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:92)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1697)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
    at com.liferay.portal.dao.orm.hibernate.DynamicQueryImpl.list(DynamicQueryImpl.java:140)
    at com.liferay.portal.dao.orm.hibernate.DynamicQueryImpl.list(DynamicQueryImpl.java:126)
    at com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl.findWithDynamicQuery(BasePersistenceImpl.java:328)
    at com.liferay.portlet.asset.service.base.AssetEntryLocalServiceBaseImpl.dynamicQuery(AssetEntryLocalServiceBaseImpl.java:154)
    at sun.reflect.GeneratedMethodAccessor713.invoke(Unknown Source)
....

I didn't paste all the log because it is quite long. Is this "Unknown entity: com.liferay.asset.entry.rel.model.impl.AssetEntryAssetCategoryRelImpl" exception is common?

I have also noted that this error only occurs when I use the projection utility. I have checked the database and and the column name that I want to project is indeed "assetEntryId".

EDIT:

I tried to:
System.out.println(AssetEntryAssetCategoryRelLocalServiceUtil.dynamicQuery(categoryQuery))

And it prints "[80710, 79157, 80725, 79248]", so I am assuming that the projection is working  correctly and the problem is when I try to use it in the AssetEntry dynamic query.

I have also found out that if I convert this projection to a list of Longs and use this list on my AssetEntry query, things work out. But would be nice to do things correctly and correct this error instead of this workaround.