Wednesday, January 21, 2015

Liferay Service Builder in Detail

Today we will see concept of Service Builder in liferay

Before reading this blog it is highly recommended to read my blog on Service Builder in Liferay .Liferay Service Builder based on hibernate and Spring DI .Service builder create tables automatically as in Hibernate.Service builder generates Services for the entities in two location. These location use the package path describe in service.xml :-

1)docroot/WEB-INF/services/package name
2)docroot/WEB-INF/src/package name

Service Builder divide the source in 2 layers:
1)An interface Layer
2)An implementation Layer

You ’ll never change anything in the interface layer manually.The implementation layer is generated in your src folder and is initially skeleton code that allows you to implement the functionality you need.

Service builder also create src folder inside META-INF folder .This folder contains all the Spring configuration files and Hibernate configuration.

Sample service.xml

<service-builder package-path="" auto-namespace-tables="true">
    <entity name="Student" local-service="true" cache-enabled="false" table="student">
        <column primary="true" name="studentId" type="int" id-type="increment"></column>
        <column name="name" type="String"></column>
        <column name="lastname" type="String"></column>
        <column name="std" type="String"></column>
        <column name="address" type="String"></column>
        <column name="phone" type="String"></column>
        <column name="gender" type="int"></column>

We will discuss each and every tag in detail

1)<service-builder package-path="" auto-namespace-tables="true">

a)service-builder package-path

This is the path of package in which all the java file is generated


By default it is false if this is true then table is created in database with a prefix of namespace.
(But if there is no table tag we see it later) 

2) <author>monu</author>

This tag is not mandatory.

Service Builder also generates Javadoc, and the name you place in the Author tags winds up in the Javadoc as the author of the code.

You can skip this tag but if you mention all Java classes have author name as mention like in this case monu.

Ex -
 * @author monu
 * @see StudentModel
 * @see
 * @see
 * @generated
public interface Student extends StudentModel, PersistedModel {


3) <namespace>Aditya</namespace>

This tag is mandatory .

By default, tables you define with Service Builder go in the Liferay database. To set them off from the rest of Liferay’s tables, you can prefix them with a namespace. This table, when created, will be called Aditya_Student in the database.

If auto-namespace-tables is true than this name is appended before table name.The entry of this namespace is also mention in database with a table name servicecomponent that contain various columns like build number ,build date.


Note :-

a)When ever you want to change the structure of table like number of columns and changes not reflect then delete entry of namespace from this table and build service again.

b)Namespace must be some valid name .Ex- Aditya9 this number 9 will create a problem in build service.

4) <entity name="Student" local-service="true" cache-enabled="false" table="student">

a)entity name="Student"

This name is compulsory but need not to be same as table name.

All Service classes are made by this name like entityLocalServiceUtil etc.
Ex- StudentLocalServiceUtil


By using cache-enabled  true data base data is cached.


This tag is not Compulsory

If you skip this tag then table in database is created with same name as entity name. Otherwise table name in database is same as table tag


By using local-service true we can use our services as web services like SOAP

5) <column primary="true" name="studentId" type="int" id-type="increment"></column>


Means this field is primary key for database.


Column is created with studentId in database


Field is of int type. We have many options like int,long,String etc.


This field is of autoincrement type. Other options are sequence,identity etc.

Some Cases:-

1)<service-builder package-path="" auto-namespace-tables="true">
    <entity name="Student" local-service="true" cache-enabled="false" table="StudentData">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>

Table name = StudentData

Reason:-Because we provide table tag so namespace has no significance here.

2)<service-builder package-path="" auto-namespace-tables="true">
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>

Table name = aditya_student (namespace_entityname)

Reason:-Because auto-namespace-table="true"

3)<service-builder package-path="" auto-namespace-tables="false">
    <entity name="Student" local-service="true" cache-enabled="false">
        <column primary="true" name="id" type="int"></column>
        <column name="name" type="String"></column>

Table name = Student

Reason:-Because  auto-namespace-table="false" and no table tag so table is created with name same as entity name.

Complsory Fields

1)<namespace>Aditya</namespace> because entry in service component
2)entity name all java files created with this

Optional Fields

2)table name="optional"

Classes Created by Service Builder











Thats it Hope this Help Someone

 Related Post:-

Service Builder in Liferay

Finder method for Service builder in liferay

Custom Sql/Query in Liferay

Custom Sql/Query with two table in Liferay

Many To Many Relationship mapping in Liferay Services


  1. Can you please guide me to create JSON service in liferay...step by step

  2. Hi harmeet just create remote service true and liferay automatically provide SOAP based web services
    ex- entity name="Student" local-service="true" remote-service="true"


Total Pageviews

Number Of Unique Visitor

Free counters!