« Back to Development (Legacy)

Finder tags in service.xml

Tags: development

Why do we need finder tags?#

findByPrimaryKey(..) methods are automatically generated for the primary keys for each entity. But if you want to find table records by another field, you will need to create a finder tag in your service.xml file.

For example, in the document library, if we have the folderId and want to get a DLFolder object, then we can use DLFolderUtil.findByPrimaryKey(folderId) method. However, if we want to get a List of all DLFolders that have a particular companyId, we will need to add a <finder> tag to our service.xml file.

An Example: portal\portal-ejb\src\com\liferay\portlet\documentlibrary\service.xml#

<entity name="DLFolder" local-service="true" remote-service="true">

<!-- PK fields -->

<column name="folderId" type="String" primary="true" />

<!-- Group instance -->

<column name="groupId" type="String" />

<!-- Audit fields -->

<column name="companyId" type="String" /> <column name="userId" type="String" /> <column name="userName" type="String" /> <column name="createDate" type="Date" /> <column name="modifiedDate" type="Date" />

<!-- Other fields -->

<column name="parentFolderId" type="String" /> <column name="name" type="String" /> <column name="description" type="String" /> <column name="lastPostDate" type="Date" />

<!-- Order -->

<order by="asc"> <order-column name="parentFolderId" /> <order-column name="name" case-sensitive="false" /> </order>

<!-- Finder methods -->

<finder name="GroupId" return-type="Collection"> <finder-column name="groupId" /> </finder> <finder name="CompanyId" return-type="Collection"> <finder-column name="companyId" /> </finder> <finder name="G_P" return-type="Collection"> <finder-column name="groupId" /> <finder-column name="parentFolderId" /> </finder> <finder name="P_N" return-type="DLFolder"> <finder-column name="parentFolderId" /> <finder-column name="name" /> </finder> </entity> }}}

  • "finder name" - after you run the build-service, for each finder tag, methods such as the "FindBy[FinderName](...)" and "FetchBy[FinderName](...)" will be automatically generated in the [EntityName]Util
  • "finder return-type" - return type for the finder method
    • "Collection" to return a List
    • "[EntityName]" to return an Object of type [EntityName]
  • "finder-column name" - the column names which will be passed in as parameters for the "FindBy" methods

Example 1: Find by a single field and return a List#

This tag in the document library's service.xml file:

		<finder name="CompanyId" return-type="Collection">
			<finder-column name="companyId" />

Will generate the following static methods in your DLFolderUtil:

	List findByCompanyId(String companyId)

	List findByCompanyId(String companyId, int begin, int end)

	List findByCompanyId(java.lang.String companyId, int begin, int end, com.liferay.util.dao.hibernate.OrderByComparator obc)

	DLFolder findByCompanyId_First(String companyId, com.liferay.util.dao.hibernate.OrderByComparator obc)

	DLFolder findByCompanyId_Last(String companyId, com.liferay.util.dao.hibernate.OrderByComparator obc)

	DLFolder[] findByCompanyId_PrevAndNext(String folderId, String companyId, com.liferay.util.dao.hibernate.OrderByComparator obc)

	void removeByCompanyId(String companyId)

	int countByCompanyId(String companyId)

Example 2: Find by multiple fields and return an Object#

This tag in the document library's service.xml file:

		<finder name="P_N" return-type="DLFolder">
			<finder-column name="parentFolderId" />
			<finder-column name="name" />

Will generate the following methods in your DLFolderUtil:

	DLFolder findByP_N(String parentFolderId, String name)

	DLFolder fetchByP_N(String parentFolderId, String name)

	void removeByP_N(String parentFolderId, String name)

	int countByP_N(java.lang.String parentFolderId, String name)
0 Attachments
Average (3 Votes)
The average rating is 5.0 stars out of 5.