Liferay ServiceBuilder Interview Questions


What is Service Builder and advantages of using it?

Access this tutorial for details:

 Liferay ServiceBuilder concepts and anatomy

 Liferay provides Service Builder tool for generating modal objects, Service layer and DAO layer.  Let’s say we have to design leave system where end user will apply for leave. In this, we can design Leave as modal object….READ MORE

Explain Service builder generated classes?

Access the below tutorial for complete details:

Liferay ServiceBuilder concepts and anatomy-part2

How to prevents automatically apply changes to database on a new version of the plugin is deployed?

build.auto.upgrade=true enables auto upgrade of database and you can find that in service.properties

Create service-ext.properties and add  build.auto.upgrade = false.

How do you enable  auto generation of primary key in the service builder?

Set Id type to “increment”

<column name="sectionId" type="long" primary="true" id-type="increment" />

What are all default services available in  <<Entity>LocalServiceImpl ?

UserLocalService,CounterLocalService,ResourceLocalService

How to inject GroupLocalService in ServiceBuilder generated services?

The reference element allows you to inject services from another service.xml within the same class loader

 <reference entity="Group" package-path="com.liferay.portal"></reference>

What is the purpose of order xml tag in service.xml?

The order element specifies a default ordering and sorting of the entities when they are retrieved from the database.

<order by="asc">
 <order-column name="startDate"></order-column>
 </order>

What types caches available in Liferay Persistence layer?

Three types of cache at persistence layer:

  • Entity
  • Finder Level cache generated by Service builder
  • Hibernate L1 & L2 cache

At Service level

  • SingleVmPool
  • MultuVMPool

How to disable entity cache in Liferay?

by setting cache-enabled to false on top of each entity. This will disable cache

How to generate service builder for existing tables in Liferay Database Schema?

service builder provides “table” and “db-name” elements to map existing tables like shown below:

 <entity name="Education" local-service="true" remote-service="false" table="education"> 
 <column name="educationId" type="long" primary="true" id-type="increment" db-name="eduId"/>
 <column name="userId" type="long" db-name="userId"></column>
 <column name="collegeName" type="String" db-name="cName"></column>
 <column name="startDate" type="Date" db-name="startDate"></column>
 <column name="endDate" type="Date" db-name="endDate"></column>
 <column name="courseName" type="String" db-name="courseName"></column>
 
 <order by="asc">
 <order-column name="startDate"></order-column>
 </order>
 <finder return-type="Collection" name="userId"> 
 <finder-column name="userId"></finder-column>
 </finder>
 </entity>

What is Custom SQL and when to use Custom SQL?

  • Liferay Service Builder generated finder methods works on single entity also.Service builder generated Finder methods executes simple queries to pull the data only, but what if your applications requires to join two tables?
  • Liferay Custom SQL is used for complex queries to get results from multiple tables  or join  multiple tables
  • Access below tutorial for Custom SQL:

Liferay custom sql example

 Liferay out of the box, provides below ways to access Database: Service Builder Finder methods Database Access API : It provides JDBC  mechanism to write SQL queries but extra burden to write POJO mappings Dynamic Query…READ MORE

Explain high level steps for implementing Custom SQL?

  • Create default.xml fine in src/custom-sql folder
  • update default.xml file with below:
<custom-sql>
    <sql id="com.javasavvy.employee.getEmployees">
    SQL query wrapped in <![CDATA[...]]>
    No terminating semi-colon
    </sql>
</custom-sql>
  • Create FinderImpl, name should be <EntityName>FinderImpl in persistence.impl package
  • FinderImpl class need to  extends BasePersistenceImpl<EntityName> and implements <EntityName>Finder.
  • <EntityFinder>Finder is interface that will generate after you run the buildService
  • Now create methods to execute the SQL Id’s that defined in default.xml
  • Refer the  <EntityName>FinderUtil in LocalServiceImpl class to access methods

What is DynamicQuery API and when to use that?

  • Liferay DynamicQuery API is used to build queries dynamically at run time
  • Liferay DynamicQuery API  uses Hibernate’s Criteria API, in which you no need to  build queries SQL and all you need to know is Objects and its variables

What are Modal Hints and how it useful?

  • Model hints are  used to specify the size of database columns and specify entity data restrictions also.
  • portlet-model-hints.xml is used to update the  modal hints
  • In portlet-model-hints.xml, we will create hint-collection as global setting which can be applied to all column with max-length etc.

What is Service Context ?

The ServiceContext class is  used to pass contextual information to  service from controller.  ServiceContext stores informations required for portlets like  tags, categories, groupId, such as permissions, tagging, categorization, and more.

Service Context also used to pass from controller to Service Layer to avoid parameters SericeContext has below parameters:

  • companyId
    portletPreferencesIds
    plid
    scopeGroupId
    userId
    uuid
    Permissions:
    addGroupPermissions
    addGuestPermissions
    deriveDefaultPermissions
    modelPermissions
    currentURL
    layoutFullURL
    layoutURL
    pathMain
    pathFriendlyURLPrivateGroup
    pathFriendlyURLPrivateUser
    pathFriendlyURLPublic
    portalURL
    remoteAddr
    remoteHost
    userDisplayURL
  • ServiceContext used to get  categories, tag names
    • serviceContext.getAssetCategoryIds(),
      serviceContext.getAssetTagNames()
       serviceContext.getAssetLinkEntryIds()
      

 

 

Please add any questions in comments if any thing missed. I will update here 🙂

One thought on “Liferay ServiceBuilder Interview Questions”

Comments are closed.