How to expose Finder methods from service to LocalServiceUtil 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 finder -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”);
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 finder -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”);
Is there no other way to get a handle on this finder method? Why do you have to copy it over in the first place?
ReplyDelete