Our Work

Custom Finders using Service builder - Liferay 6.2 / 7.X

Updated today

Liferay creates finders from service builder, in this Article we are discussing how to create custom finders in liferay 6.2/7.

This article also focuses on the below topics.

  • Enable cache for Entity
  • Enable remote services for Entity
  • We will also discuss an alternative to custom finder using Dynamic Query

Step 1: How to create custom finders in Liferay 6.2 / 7.x

  • Added custom method in StudentLocalServiceBaseImpl
public class StudentLocalServiceImpl extends StudentLocalServiceBaseImpl {
public  java.util.List<com.ktree.dbservice.model.Student>
findByGenderAndAge(int studentGender, int studentAge)
throws com.liferay.portal.kernel.exception.SystemException {
return StudentUtil.findByGender_Age(studentGender, studentAge);
}

Step 2: Show how to enable Cache for Entity

  • We can enable/disable cache in service builder generated tables.
<entity name="Student" local-service="true" remote-service="true" cache-enabled="false">

Step 3: How to enable remote services for Entity

  • Once we enable the remote services for the service builder, then it will provide the remote api to access.
<entity name="Student" local-service="true" remote-service="true" cache-enabled="false">

Step 4:Using Dynamic Query instead of Custom Finders

  • We can also write queries by using Dynamic Query as below (No need of modifications in service.xml)

http://gitlab.ktree.org/root/Proj_LiferayTraining6.2/tree/Dev_Raju/portlets/liferay_custom_service_methods-portlet/docroot/WEB-INF/src/com/ktree/dbservice/service/impl

Public List<Student> findByStudentAddress(String address){
DynamicQuery studentQuery = DynamicQueryFactoryUtil.forClass(
Student.class, PortletClassLoaderUtil.getClassLoader());
studentQuery.add(PropertyFactoryUtil.forName("studentAddress ").eq(new Integer ("Hyderabad")));
List<User> studentList=
StudentLocalServiceUtil.dynamicQuery(studentQuery);
Return studentList;
}
  • Following is the service.xml file to generate services for the Student table

<?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.ktree.dbservice">
<author>LiferayKTree</author>
<namespace>LK</namespace>
<entity name="Student" local-service="true" remote-service="true">
<!-- PK fields -->
<column name="studentId" type="long" primary="true" />
<column name="firstName" type="String" />
<column name="lastName" type="String" />
<column name="studentAge" type="int" />
<column name="studentGender" type="int" />
<column name="studentAddress" type="String" />
<!-- Order -->
<order by="asc">
<order-column name="studentId" />
</order>
<!-- Finder methods -->
<finder name="Gender" return-type="Collection">
<finder-column name="studentGender"/>
</finder>
<!-- Finder methods -->
<finder name="Gender_Age" return-type="Collection">
<finder-column name="studentGender"/>
<finder-column name="studentAge"/>
</finder>
</entity>
</service-builder>

Looking for Liferay Developer?

KTree is the best offshore Liferay development company with extensive experience in Liferay Portal Development services along with Upgrade & Migration. Hire Liferay developers or Liferay development services from LiferayDeveloper.

Request For Quote