Kombinierte Ansicht Flache Ansicht Baumansicht
Threads [ Zurück | Nächste ]
toggle
Rob Lindbloom
Join with DynamicQuery
19. Dezember 2008 14:08
Antwort

Rob Lindbloom

Rang: New Member

Nachrichten: 15

Eintrittsdatum: 16. Oktober 2008

Neue Beiträge

I'm trying to do a SQL inner join using DynamicQuery. I can't find too much information on how to do this. I'm trying to join JournalArticle with Ratings or RatingStats. Has anyone implemented this using JournalArticle.findByDynamicQuery()?

Thanks, Rob
Jonas Yuan
RE: Join with DynamicQuery
19. Dezember 2008 14:51
Antwort

Jonas Yuan

Rang: Liferay Master

Nachrichten: 993

Eintrittsdatum: 26. April 2007

Neue Beiträge

Hi Rob,

Here is a real example -getting most popular articles TOP 10 - abstracted from 1st draft of Chapter 8: Building My Community - Liferay DEV BOOK.

public List<TagsAsset> getMostPopularArticles(String companyId, String groupId, String type, int limit) {
List<TagsAsset> results = Collections.synchronizedList(new ArrayList<TagsAsset>());
DynamicQuery dq0 = DynamicQueryFactoryUtil.forClass(JournalArticle.class, "journalarticle")
.setProjection(ProjectionFactoryUtil.property("resourcePrimKey"))
.add(PropertyFactoryUtil.forName("journalarticle.companyId").eqProperty("tagsasset.companyId"))
.add(PropertyFactoryUtil.forName("journalarticle.groupId").eqProperty("tagsasset.groupId"))
.add(PropertyFactoryUtil.forName("journalarticle.type").eq("article-content"));
DynamicQuery query = DynamicQueryFactoryUtil.forClass(TagsAsset.class, "tagsasset")
.add(PropertyFactoryUtil.forName("tagsasset.classPK").in(dq0))
.addOrder(OrderFactoryUtil.desc("tagsasset.viewCount"));
try{
List<Object> assets = TagsAssetLocalServiceUtil.dynamicQuery(query);
int index = 0;
for (Object obj: assets) {
TagsAsset asset = (TagsAsset)obj;
results.add(asset);
index ++;
if(index == limit) break;
}
}catch (Exception e){
_log.debug("Error: " + e.getMessage());
return results;
}
return results;
}

Enjoy!

Jonas Yuan
Liferay Book: Liferay Portal Enterprise Intranets
Rob Lindbloom
RE: Join with DynamicQuery
19. Dezember 2008 15:25
Antwort

Rob Lindbloom

Rang: New Member

Nachrichten: 15

Eintrittsdatum: 16. Oktober 2008

Neue Beiträge

Jonus... thanks for your quick reply on a Friday no less. I'll give this a try!

Gratefully, Rob
Ruben Aguilera
RE: Join with DynamicQuery
10. Juli 2009 20:21
Antwort

Ruben Aguilera

Rang: Junior Member

Nachrichten: 47

Eintrittsdatum: 25. Februar 2009

Neue Beiträge

Hi Jonas,

First, congratulations for your new book, I have it and I think that's fantastic.

I was thinking if you could give me an example about join three tables with a dynamicquery. Specifically, I want to know how I make a join with TagsAsset, TagsEntry and TagsAssets_TagsEntries tables using a dynamicquery like example in first comment but with these three tables.

I need that to finish an important portlet in my job. Thank you very much.
Ezequiel Turovetzky
RE: Join with DynamicQuery
29. Januar 2013 15:56
Antwort

Ezequiel Turovetzky

Rang: New Member

Nachrichten: 12

Eintrittsdatum: 4. Juli 2012

Neue Beiträge

Thanks a lot for that snippet!