Message Boards

Query by joining two tables from different scope

Ronak Parekh, modified 3 Years ago.

Query by joining two tables from different scope

New Member Posts: 10 Join Date: 11/5/20 Recent Posts

Hello Team,

I want to query by joining two tables: one is portal level entity and another one is custom entity i.e JournalArticle and ArticleUserMapping entity. Please suggest how can I query by joining these two tables. I have tried using Dynamic Query but in Liferay 7.2 I am not able to create dynamic query with alias and I think without alias I won't able to query as per my requirement.

How can I use custom sql? How can I write custom sql of journal article entity joined with custom entity in custom entity's finder impl

Kindly help.

Thank you. 

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: Query by joining two tables from different scope

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts

Here's the documentation for custom sql. I hope, it helps.

https://help.liferay.com/hc/en-us/articles/360017882012-Custom-SQL

When you write the code, you can add an Impl class that matches the table, in that example EntryImpl is used.

q.addEntity("GB_Entry", EntryImpl.class);

What I did before is was to add my own class that matched the result. I used the "dummy entity trick" by Dave Nebinger. It should still work.

https://liferay.dev/blogs/-/blogs/fake-servicebuilder-entities

But I think, doing that isn't strictly necessary, I think, any class works, as long as the column names match the setters/getters. But I could be wrong here.

thumbnail
Andrew Jardine, modified 3 Years ago.

RE: Query by joining two tables from different scope

Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

.. The only thing I would add here is that the dummy entities got a little bit cleaner in 7.2 because you can now generate service builder entities without the persistence layer. So that means you don't have to worry about "disabling" the crud operations. All you have to do is make sure that you set the attribute persistence="false" on the <entity/> definition and Service Builder will do the rest .. or rather do LESS for you :)