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
Liferay 7.2 - How to query last version of JournalArticle?
Hi,
I have the following query to my JournalArticle table:
The problem is that this returns multiple instances of the same JournalArticle. After further investigation I noticed, that this is returning different versions of the same JournalArticle. How to query only the latest version of the same JournalArticle? I read somewhere that you could query a param named "head", but if I understood correctly this doesn't exist anymore on Liferay 7.x.
EDIT: I have posted this on StackOverflow too! I still didn't get an answer.
I have the following query to my JournalArticle table:
DynamicQuery journalArticleDynamicQuery = JournalArticleLocalServiceUtil.dynamicQuery();
journalArticleDynamicQuery.add(PropertyFactoryUtil.forName("DDMStructureKey").eq("MY_STRUCTURE"));
journalArticleDynamicQuery.add(PropertyFactoryUtil.forName(Field.GROUP_ID).eq(groupId));
journalArticleDynamicQuery.add(PropertyFactoryUtil.forName(Field.FOLDER_ID).eq(folderId));
journalArticleDynamicQuery.add(PropertyFactoryUtil.forName(Field.STATUS).eq(0));
journalArticleDynamicQuery.addOrder(OrderFactoryUtil.desc(Field.DISPLAY_DATE));
JournalArticleLocalServiceUtil.dynamicQuery(journalArticleDynamicQuery, 0, 30)
The problem is that this returns multiple instances of the same JournalArticle. After further investigation I noticed, that this is returning different versions of the same JournalArticle. How to query only the latest version of the same JournalArticle? I read somewhere that you could query a param named "head", but if I understood correctly this doesn't exist anymore on Liferay 7.x.
EDIT: I have posted this on StackOverflow too! I still didn't get an answer.
So there is no solution for this? I ended up using a Distinct projection and then using the API to "getLatestArticle", but this seems like a dirty solution and probably will give me problems in the future.
You can use you DQ to get just one record actually, and then use a different service call --
.. or instead of using a dynamic query, you could also use a custom search query and specify the "head" index attribute as true. Only the lastest version in the search index will be stamped with true so you won't get any of the older versions. OR! You could try to OrderBy version and just take the top record. There are many options, so I guess it just depends on what you are most comfortable with.
[code]return _journalArticleLocalService.getLatestArticle(articleId, WorkflowConstants.STATUS_APPROVED);
.. or instead of using a dynamic query, you could also use a custom search query and specify the "head" index attribute as true. Only the lastest version in the search index will be stamped with true so you won't get any of the older versions. OR! You could try to OrderBy version and just take the top record. There are many options, so I guess it just depends on what you are most comfortable with.
Hi Andrew,
Thanks a lot for your answer! I was trying to avoid doing a query for each item since I can have a lot of items. Maybe this custom search query would be a good way of doing this. Do you have any advice on how to do this? Some kind of headstart?
Thanks a lot for your answer! I was trying to avoid doing a query for each item since I can have a lot of items. Maybe this custom search query would be a good way of doing this. Do you have any advice on how to do this? Some kind of headstart?
Oh boy -- I mean, you should find the answer posted several times here in the forums (by myself and others). In fact, I have answered it so many times now that I have recorded videos on masteringlferay.com that walk through an example. If you have an account already, have a look for "Building a Custom Search Query". If you don't have an account, in the spirit of open source and helping the community, it's free to register and access most of the content. But you don't HAVE to use masteringliferay -- as I said, I'm sure you can find an example on the forums here.
I understand that you have posted this several times. Upon searching for a custom search I stumbled into the "search" method from the "JournalArticleLocalServiceUtil" API. After some tests I can query my JournalContent and this is returning only the last version if the param "version" is null. This will do for me!
Sorry to be so fast in asking for headstarts, but I was so desesperate in finding an answer that I just wanted to find it as fast as possible. Thanks a lot for your help!
Sorry to be so fast in asking for headstarts, but I was so desesperate in finding an answer that I just wanted to find it as fast as possible. Thanks a lot for your help!
No need to apologize. Time is precious, so if someone is willing to give you a head start, then why not take advantage. Let us know if you need anything else.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™