0

Since when edit a web content, liferay automatically creates new version of an article, I want to get the latest version of a specific article. I used the dynamic query as follows:

DynamicQuery query = DynamicQueryFactoryUtil.forClass(JournalArticle.class, PortletClassLoaderUtil.getClassLoader());

query.setProjection(ProjectionFactoryUtil.max("version")); 
List<JournalArticle> jList = (List<JournalArticle>)JournalArticleLocalServiceUtil.dynamicQuery(query);

I searched on google and notice ProjectionFactoryUtil.max("version") was used a lot. But in my case, exceptions have thrown:

"java.lang.Double cannot be cast to com.liferay.portlet.journal.model.JournalArticle"

Am I missing something?

ipkiss
  • 13,311
  • 33
  • 88
  • 123

2 Answers2

0

Use of ProjectionFactoryUtil.max("version") in DynamicQuery will return double value which will be maximum of field 'version'. You are trying to cast double type value to JournalArticle thats why facing this exception.

HTH Sent from mobile.

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26
  • I got it. I misunderstood the above query will return a list of Journal Article Objects. Thanks. – ipkiss Nov 10 '13 at 05:12
0

As for the intent of getting the latest version of a JournalArticle: JournalArticleLocalServiceUtil has an API method fetchLatestArticle (with a few varying parameter sets, for example to prefer published articles over non-published). In Liferay 5.x it's called getLatestArticle.

Geert
  • 3,527
  • 1
  • 20
  • 16