Need to find Max Value in Liferay table using Service Builder -
i have built liferay portlet using service builder , has 1 table. 1 of fields holds double value called 'zvalue'. need add -localserviceimpl.java file public method return maximum value found in field 'zvalue'. hoping there liferay class similar dynamicquery instead returns single value. know can return records , cycle through them myself maximum value, i'm sure there simpler way max value.
what have found in search of stackoverflow is:
dynamicquery query = dynamicqueryfactoryutil.forclass(classname); query.setprojection(projectionfactoryutil.max("zvalue"));
but didn't understand how return value dynamicquery returns list , not single value.
in following example, query journalarticle table find articles match criteria, , newest version of each of (the max).
as pankaj said, need create 2 dynamic queries. first 1 used specify need max returned:
dynamicquery subquery = dynamicqueryfactoryutil.forclass(journalarticle.class, "articlesub", portalclassloaderutil.getclassloader()) .add(propertyfactoryutil.forname("articleid").eqproperty("articleparent.articleid")) .setprojection(projectionfactoryutil.max("id"));
articlesub
alias assign query. projectionfactoryutil.max
return max.
the second 1 actual query. take value returned first query:
dynamicquery query = dynamicqueryfactoryutil.forclass(journalarticle.class, "articleparent", portalclassloaderutil.getclassloader()) .add(propertyfactoryutil.forname("id").eq(subquery)) .add(propertyfactoryutil.forname("type").eq("slider"));
the execution of dynamicquery return list<journalarticle>
matches returned generated sql sentence.
list<journalarticle> mylist = journalarticlelocalserviceutil.dynamicquery(query);
Comments
Post a Comment