Showing posts with label finder methods in liferay. Show all posts
Showing posts with label finder methods in liferay. Show all posts

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.