Dynamic Query

Though you can use custom SQL queries with Service Builder to retrieve data from the database, sometimes it’s more convenient to build queries dynamically at runtime. You can do this with Liferay’s Dynamic Query API, which wraps Hibernate’s Criteria API. The Dynamic Query API lets you build queries without writing any SQL. It helps you think in terms of objects and member variables instead of tables and columns. Complex queries can be significantly easier to understand and maintain than the equivalent custom SQL (or HQL) queries. While you technically don’t need to know SQL to construct Dynamic Queries, you still must take care to construct efficient queries. For information on Hibernate’s Criteria API, please see Hibernate’s manual. This tutorial demonstrates creating custom finders for Liferay applications using Service Builder and Dynamic Query API.

To use Liferay’s Dynamic Query API, you need to create a finder implementation for your model entity. You can define model entities in service.xml and run Service Builder to generate model, persistence, and service layers for your application. This tutorial assumes that you’re creating a Liferay application consisting of a service module, an API module, and a web module. Once you’ve used Service Builder to generate model, persistence, and service layers for your application, follow these steps to call custom finders using the Dynamic Query API:

  1. Define a custom finder method.

  2. Implement your finder using the Dynamic Query API.

  3. Add a method to your *LocalServiceImpl class that invokes your finder method.

Once you’ve taken these steps, you can access your custom finder as a service method. Note: You can create multiple or overloaded findBy* finder methods in your *FinderImpl class. Next, you’ll examine these steps in more detail.

« Custom SQLDefining a Custom Finder Method »
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています