1. Introduction
This blog describes configuration and implementation steps to expose a new OData Service based on a JPA entity and run the service as an Hana Cloud Java Application. Apache Olingo is a Java library that implements the Open Data Protocol (OData). Apache Olingo serves client and server aspects of OData. It currently supports OData 2.0 for JPA Entities. Blog also provide the way where you can provide custom pre/post processes for different type Requests on OData Services to various use cases.
For more details:
What is the Open Data Protocol (OData)? http://www.odata.org/
Why does SAP prefer OData as connectivity?
These two SCN blogs motivates the reasons why SAP prefers to use OData as connectivity:
2. Prerequisite Activities
2.1 Create and build JPA Project
Web Project is already developed with JPA Entity to consume HANA Cloud Platform Database (HANA or MaxDB). Project should have persistence.xml and class files for JPA entities as shown in below picture
2.2 Get dependent library
Add Following maven dependencies in project pom.xml to include OData Olingo and Apache CXF Jars
dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-core</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-jpa-processor-api</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-api-annotation</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.olingo</groupId>
<artifactId>olingo-odata2-jpa-processor-core</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.7.5</version>
</dependency>
3. Configuration and Implementation
3.1 Configure persistence.xml in JPA model
Open persistence.xml file from <ProjectName>/Java Resources/src/META-IN and Add the following red-marked <property> line to the persistence.xml file
propertyname="eclipselink.jpql.parser"value="org.eclipse.persistence.queries.ANTLRQueryBuilder"/>
3.2 Create required Java Classes
3.2.1 Create Factory class for Managing Connection with Database using Server Context (eg. JpaEntityManagerFactory.java)
3.2.2 Create new Java class by extending ODataJPAServiceFactory to provide means for initializing Entity Data Model (EDM) Provider and OData JPA Processors.
3.3 Configure web descriptor file (web.xml)
Configure the web application as shown below by adding the following servlet configuration to web.xml.
The Service factory (eg. LMSApprovalServiceFactory) which was implemented is configured in the web.xml of the ODataApplication as
one of the init parameters.
4. Testing OData Service
Publish and start web application (Contain JPA Entity) on HCP Account or local web server and Open the application URL and append recourse path provided in web.xml for accessing OData Service
Test Other OData operations using following tutorial
http://www.odata.org/getting-started/basic-tutorial/
5. Custom OData Processing
Most of the time in web project we need to provide custom pre and post processing with default OData processing like
- Adding additional filters or select filter to restrict usages of OData GET Request
- Manipulating results of GET Request
- Triggering some functionality in case of new Entity creation
- Filled non-provided data for Entity.
5.1 Create Custom JPA Processor for OData Request
Extend default implement class org.apache.olingo.odata2.jpa.processor.core.ODataJPAProcessorDefault for OData Processing handling with your own Custom Processor Class like eg. class CustomoDataJPAProcessor in following screen shot. and override method for different operations on case by case basis
5.1.1. Override readEntitySet Method for extending GET request to get entitySet as return - The instance variable jpaProcessor can be used to process the OData request. The jpaProcessor returns the JPA entities after processing the OData request.
5.1.2. Override readEntity method for extending GET request to get single record for entity as return - Same processing like above method
5.1.3. Override createEntity method for extending POST request to create new record for entity - create private method to manipulate entity record and add any additional processing
You can check other method in default JPA Processor class ODataJPAProcessorDefault
5.2 Changing reference from default JPA Processing with Custom Processing Class.
Write a Custom OData JPA Service Factory. Implement an OData JPA service factory to create an OData service with custom OData JPA Processor. The default service factory org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory part of the library cannot be used. Hence, create a class by extending org.apache.olingo.odata2.api.ODataServiceFactory. Follow the steps below to hook an existing flow to a custom OData JPA Processor. Copy the entire code from ODataJPAServiceFactory and replace the code as shown below.
ODataSingleProcessor odataJPAProcessor = accessFactory.createODataProcessor(oDataJPAContext);
with
ODataSingleProcessor odataJPAProcessor = new CustomODataJPAProcessor(oDataJPAContext);
5.3 Use CustomODataServiceFactory for extending your service factory class instead of using default ODataJPAServiceFactory
Change extension for class created in step 3.2.
Hope this blog will help you to expose you HCP/Java Web applications using OData based protocol which can be easily be consumed by your UI5 application and other middleware like HANA Cloud Integration.