Finder method for Service Builder in Liferay

Today we will Discuss Finder method for Service Builder in Liferay


In my Previous article Service Builder in Detail we see that liferay created basic CRUD method automatically but what if we want to add our custom method in Liferay services . There are many ways but creating Finder method is the simplest one.


Lets Start this step by step:-

Step 1:-Create service.xml
You can create service as mention in my previous article Service Builder in Detail .Today we add finder method in my Services:-

Service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.aditya">
<author>Aditya</author>
<namespace>pm</namespace>
<entity name="Student" local-service="true" remote-service="false">
<column name="sid" type="long" primary="true"></column>
<column name="name" type="String"></column>
<column name="rollno" type="int"></column>
<column name="mobileno" type="long"></column>
<order by="asc">
<order-column name="mobileno"></order-column>
</order>
<finder name="Mobile" return-type="Collection">
<finder-column name="mobileno"></finder-column>
</finder>
</entity>
</service-builder>
view raw service.xml hosted with ❤ by GitHub

Just Focus on these lines:-
<finder name="Mobile" return-type="Collection">
<finder-column name="mobileno"></finder-column>
</finder>
view raw finder.xml hosted with ❤ by GitHub

Explanation:-

1) <finder name="Mobile" return-type="Collection">
a) name="Mobile"
This is the name by which method is Created. Our method is Created by name 
findByMobile(). findBy is automatically prepended by liferay.

Note=> The name must be Capital otherwise method is created as findBymobile() 
which is not follow java camel case convention.

b) return-type="Collection"
return type of our method Collection represent List<T> in this case List<Student>.


2)<finder-column name="mobileno"></finder-column>
Column-name represent on which column you want to apply conditions.

After doing this just build your service and refresh your project.





Step 2:-Check Point

At this point liferay create our method in two places:-

1)StudentPersistence (Interface)
(under web-inf/services/com/aditya/services/persistence)

This interface contain many overloaded method of same name like:-
public java.util.List<com.aditya.model.Student> findByMobile(long mobileno)
throws com.liferay.portal.kernel.exception.SystemException;
public java.util.List<com.aditya.model.Student> findByMobile(
long mobileno, int start, int end)
throws com.liferay.portal.kernel.exception.SystemException;
public java.util.List<com.aditya.model.Student> findByMobile(
long mobileno, int start, int end,
com.liferay.portal.kernel.util.OrderByComparator orderByComparator)
throws com.liferay.portal.kernel.exception.SystemException;
public com.aditya.model.Student findByMobile_First(long mobileno,
com.liferay.portal.kernel.util.OrderByComparator orderByComparator)
throws com.aditya.NoSuchStudentException,
com.liferay.portal.kernel.exception.SystemException;
Their are other method also like removeByMobile, countByMobile etc.

2)StudentPersistenceImpl (Class)
(under web-inf/src/com/aditya/services/persistence)
This contain the implementation of StudentPersistence interface.

Step 3:-Expose method to Util Classes

Till now our method is not available to LocalServiceUtil Class.We have to expose it.Open StudentLocalServiceImpl Class. This class is just blank provide implementation of your method here so that it can be available by Util Class.

public List<Student> findByMobile(int mobileno) throws SystemException
{
return this.studentPersistence.findByMobile(mobileno);
}

As you see in implementation we just call the method from interface thats it.
Now again build services and that it .Your method is now available to StudentLocalServiceUtil Class.

You can download source code from Finder method for service builder in lifeary





Hope this will help....

Related Post:-

Liferay Service Builder in Detail

Custom Sql in Liferay

Custom Sql with two table in Liferay

Many To Many Relationship mapping in Liferay Services

Liferay Service Builder in Detail

How to install Maven

Creating Portlet/Services using Maven