Liferay Service Builder

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.  In liferay you just need to add leave modal in service.xml file. that’s it. Let go in detail level.

Why service builder ? why not Hibernate?  Yeah, No restrictions here. you can still user hibernate, JPA or any other ORM framework. In this case you have to code your self all POJO’s, SQL queries and DAO layers but service builder internally uses spring and hibernate and saves lot of development time. below are pros and cons:

Advantages:

  1. Service Builder uses Hibernate as underlying framework. you can see lot spring context files in META-INF folder
  2. Provides default cache utils
  3. Easy to develop and saves lot of development time
  4. Provides default interfaces to
  5. Manages Database connection pool internally
  6. Automatically generates required SQL scripts

Drawbacks:

  1. Liferay does not support entity relationship – Foreign key (One to Many and Many -Many relationship)
  2. Platform dependency. you can not use portlet in another environments

Service Builder vs Customer DAO implementation?

What is your preferences over Service Builder and Customer Hibernate JPA?   In one of our projects we opted to use JPA over Service Builder. We faced lot of issues and spent time on fixing performance, cache, database stalls?

Liferay OOTB plugins are using Service Builder that used Liferay platform provided connection pools and custom portlets are using JPA connection pool.  Two connection pool put lot of load database, some times , application crashes down. To fix this, we configured JNDI connection pool at tomcat level,  so that both liferay plugins and custom plugins will share same the connection pool.  but still issues with  cache and database stalls.  so think before selecting Service Builder or JPA.  Service builder is good choice than JPA

 

Lets see what are all components that service builder generates. Liferay Service builder works around service.xml files that contains all entities, finder methods, exceptions.1

  • Service.xml – define all you entites and write finder methods . below is example                                                                                   capture2
  •  modify service.xml like below:
    1. <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
      <service-builder package-path="org.javasavvy.system">
       <author>Jayaram</author>
       <namespace>js</namespace>
       <entity name="Leave" local-service="true" remote-service="false" >
       <column name="entryId" type="long" primary="true" />
       <column name="uuid" type="String"></column>
       <column name="groupId" type="long" />
       <column name="companyId" type="long" />
       <column name="userName" type="String" />
       <column name="createDate" type="Date" />
       <column name="modifiedDate" type="Date" />
       <!-- Other fields -->
       <column name="leaveName" type="String" />
       <column name="userId" type="long" />
       <column name="reason" type="String" />
       <column name="startDate" type="Date" />
       <column name="endDate" type="Date" />
       <column name="status" type="int" />
        <finder return-type="Collection" name="userId">
       <finder-column name="userId"></finder-column>
       </finder>
        <finder return-type="Collection" name="status">
       <finder-column name="status"></finder-column>
       </finder>
       <finder return-type="Collection" name="groupId">
       <finder-column name="groupId"></finder-column>
       </finder>
       <finder return-type="Collection" name="companyId">
       <finder-column name="companyId"></finder-column>
       </finder>
       </entity>
      </service-builder>
  • Service builder generated classes
    1. portlet-impl classes  –  generates modal, service and persistence implementation classes.
    2. portlet-service classes –  generates {portlet-name}-service.jar files that can be found in lib folder and source code as  service folder under WEB-INF folder
    3. It also generates spring configuration   under META-INF folder
    4. service.properties –  this holds build number, auto upgrade, namespace.                                                                                        Note: Do not edit service.properties as it is auto generated and create service-ext.properties at same location to over write 

Finally it generates below directory structure:

Capture3

Click here for Part-2

Liferay ServiceBuilder concepts and anatomy-part2

In the previous tutorial, we have gone through service builder and its component. In this we will talk about below  generated classes and its relationships: service.xml components Modal Classes Service Classes Persistence Classes Spring and Hibernate configuration Modal Layer:  …READ MORE

1 Comment

  1. Pingback: Liferay ServiceBuilder concepts and anatomy-part2

Leave a Comment

Your email address will not be published. Required fields are marked *