Showing posts with label ServiceBuilder in Liferay 7. Show all posts
Showing posts with label ServiceBuilder in Liferay 7. Show all posts

Saturday, 17 May 2025

Using Service Builder in Modular Liferay Projects (OSGi Modules)

Using Service Builder in Modular Liferay Projects (OSGi Modules)

Service Builder is one of the most powerful tools provided by Liferay for generating database interaction layers. In modular OSGi-based Liferay projects, it allows you to define entities and auto-generate service layers that are ready for use across modules.

๐Ÿงฐ Prerequisites

  • Liferay 7.x or Liferay DXP workspace
  • Blade CLI installed
  • Basic understanding of OSGi modular structure

๐Ÿ“ฆ Step 1: Create Service Builder Module

blade create -t service-builder -p com.example.sb -c Product product-sb

This creates 3 modules:

  • product-sb-api – Interface layer
  • product-sb-service – Implementation layer
  • product-sb – Portlet or UI module (optional)

๐Ÿ“ Step 2: Define Entity in service.xml

Edit product-sb-service/src/main/resources/META-INF/service.xml

<?xml version="1.0"?>
<service-builder package-path="com.example.sb">

  <author>YourName</author>

  <namespace>SB</namespace>

  <entity name="Product" local-service="true" remote-service="false">
    <column name="productId" type="long" primary="true" />
    <column name="name" type="String" />
    <column name="price" type="double" />
  </entity>

</service-builder>

⚙️ Step 3: Build Services

./gradlew buildService

This will generate:

  • Persistence layer
  • Local service interfaces and implementations
  • ServiceUtil classes for easy access

๐Ÿ”Œ Step 4: Use the Generated Services

In any other OSGi module, include a dependency on the API module:

dependencies {
    compileOnly project(":modules:product-sb:product-sb-api")
}

Inject the local service using OSGi annotations:

@Reference
private ProductLocalService productLocalService;

Then you can use the service like this:

Product product = productLocalService.createProduct(counterLocalService.increment());
product.setName("Phone");
product.setPrice(499.99);
productLocalService.addProduct(product);

๐Ÿš€ Step 5: Deploy and Test

Deploy all the modules using:

./gradlew deploy

Use Gogo shell or custom portlet to test service calls.

๐Ÿงช Example Gogo Shell Test

:load com.example.sb.service.ProductLocalService
productLocalService.getProducts(0, 10)

๐Ÿ“˜ Summary

  • Service Builder works well in modular OSGi Liferay projects.
  • You can generate entities and services with a simple XML.
  • Split code into clean layers: API and SERVICE.
  • Can be consumed from any OSGi portlet or module.

๐Ÿ”— Useful Resources

Adding Custom Finder Methods in Liferay Service Builder

Adding Custom Finder Methods in Liferay Service Builder

Adding Custom Finder Methods in Liferay Service Builder

Liferay’s Service Builder is a powerful tool for creating database-backed services. In many real-world applications, you need to fetch data using custom criteria. This guide walks you through creating custom finder methods using Liferay's Service Builder with full detail.

๐Ÿงฐ Prerequisites

  • Liferay Workspace (7.x or DXP)
  • Blade CLI or IDE (e.g., IntelliJ or Eclipse)
  • Basic knowledge of Service Builder

๐Ÿ“ฆ Step 1: Create a Service Builder Module

blade create -t service-builder -p com.example.custom -c Example example-finder

This will create three modules:

  • example-finder-api
  • example-finder-service
  • example-finder

๐Ÿ“ Step 2: Define the Entity in service.xml

Edit example-finder-service/src/main/resources/META-INF/service.xml:

<entity name="Employee" local-service="true" remote-service="false" table="Employee">
  <column name="employeeId" type="long" primary="true" />
  <column name="name" type="String" />
  <column name="department" type="String" />
</entity>

After updating, run:

./gradlew buildService

๐Ÿ” Step 3: Create a Custom Finder Method

➡️ 3.1 Create Method in EmployeeFinder Interface

File: example-finder-api/src/main/java/com/example/custom/service/persistence/EmployeeFinder.java

package com.example.custom.service.persistence;

import java.util.List;
import com.example.custom.model.Employee;

public interface EmployeeFinder {
    List<Employee> findByDepartment(String department);
}

➡️ 3.2 Implement in EmployeeFinderImpl

File: example-finder-service/src/main/java/com/example/custom/service/persistence/impl/EmployeeFinderImpl.java

package com.example.custom.service.persistence.impl;

import com.example.custom.model.Employee;
import com.example.custom.service.persistence.EmployeeFinder;
import com.liferay.portal.kernel.dao.orm.*;
import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
import org.osgi.service.component.annotations.Component;

import java.util.List;

@Component(service = EmployeeFinder.class)
public class EmployeeFinderImpl extends BasePersistenceImpl<Employee> implements EmployeeFinder {

    public List<Employee> findByDepartment(String department) {
        Session session = null;

        try {
            session = openSession();

            String sql = "SELECT * FROM Employee WHERE department = ?";
            SQLQuery query = session.createSQLQuery(sql);
            query.setCacheable(false);
            query.addEntity("Employee", getModelClass());
            query.setString(0, department);

            return query.list();

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to execute custom finder", e);
        } finally {
            closeSession(session);
        }
    }
}

๐Ÿ”— Step 4: Access Custom Finder from LocalServiceImpl

Use the finder in EmployeeLocalServiceImpl.java:

public List<Employee> getEmployeesByDepartment(String department) {
    return employeeFinder.findByDepartment(department);
}

๐Ÿ“Œ Step 5: Build and Deploy

./gradlew buildService
./gradlew deploy

๐Ÿงช Step 6: Test Your Custom Finder

Call your service method using:

  • REST endpoint (if exposed)
  • JSP/React portlet
  • Integration test

๐Ÿ“˜ Summary

  • We created a custom finder in Liferay using Service Builder
  • Used raw SQL and the BasePersistenceImpl class
  • Injected and called it in the service layer

๐Ÿ”— Useful Links

Wednesday, 21 December 2016

CRUD Operations in Liferay 7

Today we will see how to perform the CRUD operation in Liferay 7. I assume you know how to create mvc portlet module and servicebuilder module in Liferay.

Following are some simple steps to execute CRUD operation in LiferayDXP.

Step1 Create MVC portlet module with name ProductRegistration and Component class name ProductRegistrationPortlet and package name com.liferay.product.portlet.

Tuesday, 20 December 2016

Custom SQL in Liferay 7

Today we will see how to write custom sql query in liferay.


Liferay service builder generates basic basic CRUD method for single entity.But their are certain scenarios such as fetching data from multiple table, in such cases we have to use custom sql.


If you are not familiar with creating servicebuilder module please refer my blog on Creating ServiceBuilder in liferay 


Creating Service Builder in Liferay 7

We can create service builder module to perform various CRUD operation in liferay. This post includes creating and building service module in liferay 7.

In service module we just need to specify table structure in service.xml file and all methods for CRUD operation will generate automatically.

Today we will create basic service module with only one table i.e one entity using Liferay IDE