Using Domain-Specific Language Queries
Liferay 7.4+
DSL Query is an acronym for Domain-Specific Language Query. The foundation is based upon using a domain-specific language for the implementation. This makes the writing of queries more natural than Dynamic Query or Custom SQL.
Deploy an Example
Start a new Liferay instance by running
docker run -it -m 8g -p 8080:8080 liferay/portal:7.4.3.120-ga120
Sign in to Liferay at http://localhost:8080. Use the email address test@liferay.com and the password test. When prompted, change the password to learn.
Then, follow these steps to deploy the example:
-
Download and unzip the
liferay-g4e1.zip
example project.curl https://resources.learn.liferay.com/dxp/latest/en/liferay-development/building-applications/data-frameworks/advanced-queries/liferay-g4e1.zip -O
unzip liferay-g4e1.zip
-
Build and deploy the project module.
cd liferay-g4e1
./gradlew deploy -Ddeploy.docker.container.id=$(docker ps -lq)
NoteThis command is the same as copying the deployed jars to
/opt/liferay/osgi/modules
on the Docker container. -
Confirm the deployment in the Liferay Docker container console.
STARTED com.liferay.g4e1.api_1.0.0 [1461] STARTED com.liferay.g4e1.service_1.0.0 [1462] STARTED com.liferay.g4e1.web_1.0.0 [1463]
-
To verify the example module is working, open your browser to
https://localhost:8080
. -
add the g4e1 portlet to a page. you can find the example portlet under sample in widgets.
-
Add an entry by entering a name and a description. Leave Hidden unchecked. Click Add, and the new entry appears under G4E1 Entries.
-
Add another entry with a different name and description. This time, check Hidden. The new entry doesn’t appear under G4E1 Entries.
This example uses DSL Query to retrieve only entries with a specified value in the database (hidden_ = false
).
Building Queries with DSLQuery
- Open
G4E1EntryLocalServiceImpl.java
. The code for the DSL Query is defined in thegetG4E1Entries
method.
@Override
public List<G4E1Entry> getG4E1Entries(boolean hidden) {
DSLQuery query = DSLQueryFactoryUtil.select(
).from(
G4E1EntryTable.INSTANCE
).where(
G4E1EntryTable.INSTANCE.hidden.eq(hidden)
);
return dslQuery(query);
}
-
You can create a DSL Query using
DSLQueryFactoryUtil
’s static methods. This example makes a simpleSELECT
statement using theselect()
method to retrieve all data from the chosen rows in the table.NoteDSLQueryFactoryUtil
also contains other methods such asselectDistinct()
,count()
, andcountDistinct()
for more complex SQL operations. -
Choose the table to query using the
from()
method. It takes a single argument in the form of aTable
object. -
From there, you can continue to build onto your custom query using Java methods for most SQL operations. See the
JoinStep
javadocs for a list of all possible operations. This example uses aWHERE
clause to check if the value ofhidden
is equal to the given boolean variable.NoteThe DSL Query can handle things like unions, joins, aliases, functions, complex where clauses,
group by
s sandsort by
s, all using this type of domain-specific language to build queries.
DSLQuery
or DynamicQuery
All Service Builder code in the portal’s services and your own custom services have built-in support for DSL Query, so you can handle your entities like core entities. Since DSL Query is implemented in Java classes and interfaces, the queries are validated at compile time instead of at runtime, making it less error-prone than Dynamic Query.
In the most recent versions of Liferay, DSL Query is the best way to handle custom queries for all Service Builder entities. It is more intuitive, efficient, and less error-prone than equivalent Dynamic Query/Custom SQL implementations.