Accessing Your Custom Finder Method from the Service Layer

So far, you’ve created a *FinderImpl class, defined a findBy* finder method in that class, and implemented the finder method using Dynamic Query or custom SQL. Now how do you call your finder method from the service layer?

When you ran Service Builder (if you haven’t run it yet; run it now), the *Finder interface was generated (e.g., GuestbookFinder). For proper separation of concerns, only a local or remote service implementation (i.e., *LocalServiceImpl or *ServiceImpl) in your service module should invoke the *Finder class. The portlet classes in your application’s web module invoke the business logic of the services published from your application’s service module. The services, in turn, access the data model using the persistence layer’s finder classes.

You’ll add a method in the *LocalServiceImpl class that invokes the finder method implementation via the *Finder class. Then you’ll rebuild your application’s service layer so that the portlet classes and JSPs in your web module can access the services.

For example, for the Guestbook application, you’d add the following method to the GuestbookEntryLocalServiceImpl class:

public List<GuestbookEntry> findByEntryNameGuestbookName(String entryName,
    String guestbookName) throws SystemException {

    return entryFinder.findByEntryNameGuestbookName(String entryName,
        String guestbookName);

After you’ve added your findBy* method to your *LocalServiceImpl class, run Service Builder to generate the interface and make the finder method available in the EntryLocalService class.

Now you can indirectly call the finder method from your portlet class or from a JSP by calling _entryLocalService.findByEntryNameGuestbookName(...)!

Congratulations on following the process of developing custom queries in a custom finder and exposing it as a service for your portlet!

Creating Local Service

Invoking Local Services

« Dynamic QueryActionable Dynamic Queries »
¿Fue útil este artículo?
Usuarios a los que les pareció útil: 0 de 0