Skip the Headless API: Manage Liferay Objects Efficiently with ObjectEntryManager

Learn when and how to use ObjectEntryManager for faster, cleaner backend Object operations in OSGi modules

Laxit Khanpara
Laxit Khanpara
読了時間: 2 分

Introduction

Liferay Objects provide a flexible way to model and manage custom data without relying on traditional Service Builder modules. In most implementations, developers interact with Objects using Headless APIs such as:

/o/c/{objectName}

This approach is well-documented and works effectively for frontend applications and external integrations.

However, when working inside backend modules, there is an alternative approach that can improve efficiency and reduce unnecessary overhead:

Using ObjectEntryManager directly from the service layer.

This article explains how to use ObjectEntryManager to create and retrieve Object entries, along with guidance on when this approach is appropriate.


Understanding ObjectEntryManager

ObjectEntryManager is part of Liferay’s internal service layer responsible for handling Object operations. It supports:

  • Creating Object entries
  • Retrieving data
  • Updating entries
  • Deleting records
  • Performing search and filtering

It is important to note that Headless APIs internally rely on this service layer. Using ObjectEntryManager allows backend code to interact with Objects directly without making HTTP requests.


Required Classes and Interfaces

To work with Object entries programmatically, the following Liferay components are used:

import com.liferay.object.model.ObjectDefinition;
import com.liferay.object.rest.dto.v1_0.ObjectEntry;
import com.liferay.object.rest.manager.v1_0.ObjectEntryManager;
import com.liferay.object.rest.manager.v1_0.ObjectEntryManagerRegistry;
  • ObjectDefinition represents the Object schema
  • ObjectEntry represents the Object data
  • ObjectEntryManager provides CRUD operations
  • ObjectEntryManagerRegistry resolves the appropriate manager implementation

Step 1: Inject ObjectEntryManagerRegistry

@Reference
private ObjectEntryManagerRegistry objectEntryManagerRegistry;

Step 2: Retrieve ObjectDefinition

The Object Definition identifies which Object you are working with:

ObjectDefinition objectDefinition = ObjectDefinitionLocalServiceUtil.getObjectDefinitionByExternalReferenceCode("YOUR_OBJECT_ERC", companyId);

Step 3: Obtain ObjectEntryManager

ObjectEntryManager objectEntryManager = objectEntryManagerRegistry.getObjectEntryManager(objectDefinition.getStorageType());

Step 4: Create an Object Entry

Map<String, Serializable> values = new HashMap<>(); 
values.put("name", "John Doe"); 
values.put("email", "john@example.com"); 
long userId = contextUser.getUserId(); // or themeDisplay.getUserId() 
ObjectEntry objectEntry = objectEntryManager.addObjectEntry( userId, objectDefinition, values, null, null );

Step 5: Retrieve Object Entries

Retrieve a Single Entry

ObjectEntry entry =  objectEntryManager.getObjectEntry( objectDefinition, entryId );

Retrieve Multiple Entries

Page<ObjectEntry> page = objectEntryManager.getObjectEntries(objectDefinition, null, null, null, Pagination.of(1, 10) );

Filtering and Pagination

Pagination can be applied to control result size:

Pagination pagination = Pagination.of(1, 20);

Page<ObjectEntry> page = objectEntryManager.getObjectEntries( objectDefinition, null, null, null, pagination);

Additional filtering and sorting options can be incorporated depending on the use case.


Performance Considerations

Using ObjectEntryManager avoids the overhead of HTTP communication.

Headless API flow:

Application → HTTP Request → Headless API → Service Layer → Database

Service layer flow:

Application → ObjectEntryManager → Database

By eliminating the HTTP layer, backend operations can execute more efficiently.


Permissions and Security

ObjectEntryManager enforces permissions based on the user context provided.

long userId = contextUser.getUserId();

Key considerations:

  • Always use the current user context
  • Ensure roles have appropriate Object permissions
  • Avoid using administrative users unless necessary

Permissions are not bypassed; they are evaluated based on the provided user.


When to Use ObjectEntryManager

This approach is suitable when:

  • Working within a Liferay OSGi module
  • Implementing backend business logic
  • Performance is a concern
  • Avoiding internal API calls is desirable

When Not to Use It

Avoid this approach when:

  • Building public or external APIs
  • Frontend applications rely on REST endpoints
  • OAuth-based access is required
  • A loosely coupled architecture is preferred

Recommended Approach

A balanced architecture typically combines both approaches:

Use Case Approach
Frontend or external systems Headless APIs
Backend processing and internal logic ObjectEntryManager

Conclusion

ObjectEntryManager offers a direct and efficient way to work with Liferay Objects from within backend modules. While Headless APIs remain essential for external communication and frontend integration, using the service layer directly can improve performance and simplify internal processing.

Choosing the appropriate approach based on the use case is key to building scalable and maintainable Liferay applications.

 

ページコメント

Related Assets...

結果が見つかりません。

More Blog Entries...