Liferay Service Builder CRUD Example

Liferay provides a code generation tool called Service builder, that allows developers to define object models called entities. Service builder generates the service layer, persistence layer and required model codes and helps the developer to concentrate more on business logic implementation. It creates hibernate and spring configuration to connect to the database.

Here is a simple CRUD operation example using the Liferay service builder.

  • Create a Liferay plugin project called “service-builder-crud-portlet“.
  • Create a service.xml file under the WEB-INF folder of the plugin project. This can be achieved in Liferay IDE(LDS) from New >> Liferay service builder and specifying plugin-project, under which the service.xml file needs to be created.
  • Add the following code to service.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC 
"-//Liferay//DTD Service Builder 6.2.0//EN" 
<service-builder package-path="com.asb.test">

 <entity name="Student" local-service="true" remote-service="false">

 <!-- PK fields -->
 <column name="studentId" type="long" primary="true" />
 <!-- Group instance -->
 <column name="groupId" type="long" />
 <!-- Audit fields -->

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

 <!-- Other fields -->
 <column name="studentName" type="String" />
 <column name="studentGrade" type="String" />
 <column name="Gender" type="String" />
 <column name="age" type="int" />

 <!-- Order -->
 <order by="asc">
 <order-column name="studentName" />

 <!-- Finder methods -->
 <finder name="studentName" return-type="Collection">
 <finder-column name="studentName" />
 <finder return-type="Colection" name="age">
 <finder-column name="age"></finder-column>
  • Here, in the above XML file, we have added an entity called Student. We have also added few columns for the entity.
  • Now right click on project >> Liferay >> Build services. This will create all the codes required for database operations. Note that the service should build without any errors.
  • Now to implement CRUD operation, we will create a simple portlet called StudentPortlet, by rightclik >> New >> Liferay Portlet. Select com.liferay.util.bridges.mvc.MVCPortlet as portlet’s super class and click on finish.
  • Add view.jsp page with following code:
<%@page import="com.liferay.taglib.portlet.ActionURLTag"%>
<%@page import="com.asb.test.service.StudentLocalServiceUtil"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="" prefix="aui" %>
<%@ taglib uri="" prefix="liferay-portlet" %>
<%@ taglib uri="" prefix="portlet" %>
<%@page import="com.asb.test.model.Student" %>
<%@page import="java.util.List" %>
<portlet:defineObjects />

List<Student> studentList = StudentLocalServiceUtil.getStudents(-1, -1);

<liferay-portlet:actionURL var="addStudentAction" name="addStudent">
 border:solid; border-width: 1px; width: 15%; text-align:center;"
 border:solid; border-width: 1px; width: 10%; text-align:center;"
 border:solid; border-width: 1px; width: 100%; text-align:center;"
.control-group .input-text-wrapper{
<table class="myTable3">
<tr class="myTable3">
 <th class="myTable">student Name</th>
 <th class="myTable">student Grade</th>
 <th class="myTable">Gender</th>
 <th class="myTable">Age</th>
 <th class="myTable1">Edit</th>
 <th class="myTable1">Delete</th>
 <%for(Student st : studentList)
<tr class="myTable3"> 
 <td class="myTable"><%=st.getStudentName() %></td>
 <td class="myTable"><%=st.getStudentGrade() %></td>
 <td class="myTable"><%=st.getGender() %></td>
 <td class="myTable"><%=st.getAge() %></td>
<liferay-portlet:renderURL var="editStudent">
 <portlet:param name="jspPage" value="/jsp/editStudent.jsp" />
 <portlet:param name="name" value="<%=st.getStudentName() %>"/>
 <portlet:param name="grade" value="<%=String.valueOf(st.getStudentGrade()) %>"/>
 <portlet:param name="gender" value="<%=st.getGender() %>"/>
 <portlet:param name="age" value="<%=String.valueOf(st.getAge()) %>"/>
 <portlet:param name="stId" value="<%=String.valueOf(st.getStudentId()) %>"/>
 <td class="myTable1">
 <aui:button name="" value="Update" onClick="<%=editStudent %>" ></aui:button>
 <portlet:actionURL name="deleteStudent" var="deleteStudent"></portlet:actionURL>
 <aui:form action="<%=deleteStudent %>" method="post" class="myTable1">
 <aui:input name="delStId" type="hidden" value="<%=st.getStudentId() %>"/>
 <td class="myTable1"> 
 <aui:button name="" type="submit" value="Delete" />
<%} %>
</table><span style="border:solid; border-width:1px;
padding:5px; text-align:center;">
<h5 style="text-align:center;">Add New Student</h5>
<aui:form action="<%=addStudentAction %>" method="post" name="name">
 <aui:input name="name" type="text" value=""/>
 <aui:input name="gender" type="text" value=""/>
 <aui:input name="age" type="number" value="" />
 <aui:input name="grade" type="number" value="" />
 <aui:input name="" type="submit" value="Add Student" />

Here, we are getting the data of student entity from the database using StudentLocalServiceUtil.getStudents(-1, -1) method generated by running service builder.

We display the available records in a table. We also have a form to add a new student entity to the database. We also have options to edit and delete any record.

Each button is bound to a particular action in the back end. On the click of any of these buttons, corresponding action methods are executed.

  • Now Add a JSP page called editStudent.jsp to provide an edit form with following code:
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ taglib uri="" prefix="aui" %>
<%@ taglib uri="" prefix="liferay-portlet" %>
<%@ taglib uri="" prefix="portlet" %>


<portlet:actionURL name="updateStudent" var="editStudentAction">

 String studentName = renderRequest.getParameter("name");
 String age = renderRequest.getParameter("age");
 String gender = renderRequest.getParameter("gender");
 String grade = renderRequest.getParameter("grade");
 String stId = renderRequest.getParameter("stId");

<h3>Edit Student: <%= studentName%></h3>

<aui:form action="<%=editStudentAction %>" method="post" name="name">
 <aui:input name="name" type="text" value="<%=studentName%>"/>
 <aui:input name="gender" type="text" value="<%=gender %>"/>
 <aui:input name="age" type="number" value="<%=Integer.parseInt(age) %>"/>
 <aui:input name="grade" type="number" value="<%=Integer.parseInt(grade) %>" />
 <aui:input name="stId" type="hidden" value="<%=Long.parseLong(stId) %>"/> 
 <aui:input type="submit" value="Update" name="update"></aui:input>
  • This page will contain the corresponding values of the record, which is to be edited. On clicking the update button, new values are stored in the database.
  • Now implement the action methods for add, update and delete actions, by adding following code under portlet’s action class:
package com.asb.test.portlet;
import com.asb.test.model.Student;
import com.asb.test.model.impl.StudentImpl;
import com.asb.test.service.StudentLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
public class StudentPortlet extends MVCPortlet {
 public void addStudent(ActionRequest actionRequest, 
ActionResponse actionResponse) 
 throws SystemException, PortalException 
   long studentId=CounterLocalServiceUtil.increment();
   Student student=null;
   student.setStudentName(ParamUtil.getString(actionRequest, "name"));
.getInteger(actionRequest, "grade"))));
   student.setGender(ParamUtil.getString(actionRequest, "gender"));
   student.setAge(ParamUtil.getInteger(actionRequest, "age"));
 public void deleteStudent(ActionRequest actionRequest, 
ActionResponse actionResponse)
 throws SystemException, PortalException
 public void updateStudent(ActionRequest actionRequest, 
ActionResponse actionResponse)
 throws SystemException, PortalException
   Student student=StudentLocalServiceUtil
.fetchStudent(ParamUtil.getInteger(actionRequest, "stId"));
   student.setStudentName(ParamUtil.getString(actionRequest, "name"));
(ParamUtil.getInteger(actionRequest, "grade"))));
   student.setGender(ParamUtil.getString(actionRequest, "gender"));
   student.setAge(ParamUtil.getInteger(actionRequest, "age"));

Here, we have different methods for add, update and delete records. In the add action method, we are getting new studentId from CounterLocalServiceUtil.increment() and creating an object with values that are retrieved from the user from. This is  later persisted in the database using the addStudent() method, which is generated by service builder.

Similarly, we are using service builder generated deleteStudent() and updateStudent() methods for delete and update operations.

Below are the images of output:

Add student form and results with update and delete options:


Edit Page:


Here is the database stored values:


Note: Version used: Liferay 6.2 + Apache Tomcat 7.

You may also interested in