Thursday, June 9, 2011

Liferay Service Builder : Part 1 (Finder methods in liferay portal)

In liferay portal if we want to find records from table then we need finder methods. To create finder method we need to specify the finder tag in the service.xml file.

Assume that we have a database table named customer having fields like name, city, age etc. We want to find all the customers belonging to a particular city.

Following are the steps to create finder method for the above requirement-

1.  In the service.xml file add the following line after database columns declarations,

<finder name="City" return-type="Collection">
<finder-column name="city"></finder-column>
</finder>


Name – method name , a method named findByCity will be generated

Collection – return type

Finder column name – name of the corresponding column in the customer table

2.  Run the ant target "ant build-service". In the SampleUtil.java you will find the following method –

public static java.util.List<com.sample.model.Sample> findByCity(
java.lang.String city) throws com.liferay.portal.SystemException {
return getPersistence().findByCity(city);
}


3.  Copy the finder method from this class and paste it inside the SampleLocalServiceImpl class and modify the method definition as following

-       Remove the static modifier

-       Replace getPersistence() with SampleUtil.

4.  Run the target "ant build-service" again. After build finder will be available in your SampleLocalServiceUtil class.

List<Sample> customerList = SampleLocalServiceUtil.findByCity("cityname");

NOTE: Replace Sample with your EntityName.

7 comments:

  1. What are the different return types?

    ReplyDelete
  2. [...] hi Sunil,if i understand, you want to get user by ssn, you will want to have a method in the UserExtratLocalServiceUtil named findBySSN that return a UserExtrat with w given ssn number, if you want this, i think this link will get you the solution nchAllah follow this link [...]

    ReplyDelete
  3. Depends on what data types you have in table for this entity

    ReplyDelete
  4. Hi!
    There is one more thing to consider. I assume your city is a String so such configured, your generated finder implementation findByCity(city) would perform exact mach, what means it would return all cities with same name - and I doubt someone want collection with one entry.

    To have more natural finder there is option to define way liferay service engine will perform comparison:


    This way liferay service would build sql queries using LIKE (not =) comparison and it would append % at the end of search string.

    ReplyDelete
  5. Hm.. my xml code is excluded:
    finder-column name=”city” comparator="LIKE"

    ReplyDelete
  6. hi it's very useful,thanks.
    by the way what version of liferay did you use in this example.
    regards.

    ReplyDelete
  7. hello follow the process but at the point of (List customerList = SampleLocalServiceUtil.findByCity ("cityname");) no where to put this line.
    I have to edit my view.jsp to add a button or text field?
    HelpMe

    ReplyDelete