由于编辑网页内容时,liferay会自动创建文章的新版本,我想获取特定文章的最新版本。我使用动态查询如下:
DynamicQuery query = DynamicQueryFactoryUtil.forClass(JournalArticle.class, PortletClassLoaderUtil.getClassLoader());
query.setProjection(ProjectionFactoryUtil.max("version"));
List<JournalArticle> jList = (List<JournalArticle>)JournalArticleLocalServiceUtil.dynamicQuery(query);
我在谷歌搜索并注意ProjectionFactoryUtil.max("version")
已经使用了很多。但就我而言,抛出异常:
"java.lang.Double cannot be cast to com.liferay.portlet.journal.model.JournalArticle"
我错过了什么吗?
答案 0 :(得分:0)
在DynamicQuery中使用ProjectionFactoryUtil.max("version")
将返回double值,该值将是字段'version'的最大值。您正在尝试将双重类型值转换为JournalArticle,这就是为什么要面对此异常。
HTH 从手机发送。
答案 1 :(得分:0)
至于获取JournalArticle的最新版本的意图:#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
string text;//partial text for each whitespace
string finalText;//String that has been concat
int totalLetter=0;//Total number of all letter
double storedValue[26];//Giving Result to each array
int i;//"for" statement counter declaring
int j;//"for" statement second counter declaring
int letters[26];//array to reducing ascii value on line 26
cout << "Enter the text at the prompt to calculate letter frequencies. " << endl <<
"Enter DONE when finished with the input."<< endl;
while(text!="DONE"){//check the input, if it's not "DONE" then continue adding string
cout<< "Enter a line of a text: ";
std::getline(std::cin,text);
if(text=="DONE")break;
finalText = finalText + text;
}
char *charArray = new char[finalText.length()+1];//Converting string input to char type
std::strcpy(charArray,finalText.c_str());//Copying string to char
for(i=0; i<finalText.length(); i++){//reduce to smaller case
if(charArray[i]>='A' && charArray[i]<='Z')
charArray[i]=charArray[i]+32;
}
for(i=0; i<finalText.length(); i++){//set default for charArray to be 0
if(charArray[i]<'a' && charArray[i]>'z'){
charArray[i]=0;
}
}
for(i=0; i<finalText.length(); i++){//calculate the total characters input
if(charArray[i]>='a' && charArray[i]<='z'){
totalLetter++;
}
}
for(i=0; i<26; i++){//Set all storeValue to be 0 from a - z
storedValue[i] = 0;
}
for(i=0; i<finalText.length(); i++){//convert letters to start at 0 as an 'a' from ascii code and so on
letters[i]=(int)charArray[i]-97;
storedValue[i] = 0;
}
for(i=0; i<finalText.length(); i++){//increment value in array for each letter
for(j=0; j<26; j++){
if(letters[i]==j)
storedValue[j]++;
}
}
cout << endl;
cout << "A total of " << totalLetter << " letters of the alphabet were processed" << endl;
cout << endl;
cout << "a b c d e f g h i j k l m n o p q r s t u v w x y z %"<<endl;
//calculate the percent
for(i=0; i<26; i++){
storedValue[i]=round((storedValue[i]/totalLetter)*100);
}
cout<<endl;
//Get Maximum Percent
int maxPercent=0;
int maxLetterPercent=0;
for(i=0; i<25; i++){
if(storedValue[i]>maxPercent){
maxPercent=storedValue[i];
maxLetterPercent=i;
}
}
//Printing asterisk
for(i=0; i<maxPercent; i++){
for(j=0; j<26; j++){
if(storedValue[j]>0)
cout<<"* ";
else if(storedValue[j]<=maxPercent)
cout<<" ";
}
cout<<" "<<i+1;
cout<<endl;
}
char finalCharMax = maxLetterPercent + 'a';
cout<<"The most common letter is " << finalCharMax << " with a frequency of " << maxPercent
<< " %";
cout<<endl;
return 0;
}
有一个API方法fetchLatestArticle
(有一些不同的参数集,例如更喜欢已发表的文章而非未发布的文章)。
在Liferay 5.x中,它被称为getLatestArticle
。