Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

The aim of this blog is to expose a new Odata Service based on a JPA entity and run the service as an Hana Cloud Java Application.

It must be considered merely an exercise, since normally the odata services are provided by backend systems.

In this blog i'll use the JPAProducer API provided by odata4j library.

For the excercise we'll use the "persistence-with-ejb" sample, delivered with Hana Cloud Platform SDK. I've used version 2.x

First of all we need to get odata4j. I prefered to get the latest source version. Here the commands to build and install via maven.

cd ~/apps/libs
git clone https://code.google.com/p/odata4j/
cd odata4j
mvn clean compile install

At thispoint odata4j is installed in local maven repository.

Next step is to configure maven dependencies in eclipse project:

Odata4j can produce odata services using jersey or cxf.

We're going to use jersey.

Add the following dependencies in <workspace>/persistence-with-ejb/pom.xml

<dependency>
  <groupId>org.odata4j</groupId>
  <artifactId>odata4j-core</artifactId>
  <version>0.8.0-SNAPSHOT</version>
  <type>pom</type>
</dependency>
<dependency>
  <groupId>org.odata4j</groupId>
  <artifactId>odata4j-jersey</artifactId>
  <version>0.8.0-SNAPSHOT</version>
</dependency>

Then copy the odata4j-dist-0.8.0-SNAPSHOT.jar file located in local maven repository (org/odata4j/odata4j-dist/0.8.0-SNAPSHOT/) in WebContent/WEB-INF/lib directory of the eclipse project.

NB: If you're using HANA as database you have also to copy the jar file com.sap.core.persistence.osgi.hdb.platform_x.x.x.rt2.jar that is located in HCP_SDK/repository/plugins/ into WebContent/WEB-INF/lib.

Next we need to to create the Odata Producer.

I'll use the standard org.odata4j.producer.jpa.JPAProducer. Here the code for the factory:

package com.sap.cloud.sample.odata;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.odata4j.producer.ODataProducer;
import org.odata4j.producer.ODataProducerFactory;
import org.odata4j.producer.jpa.JPAProducer;
public class PersonProducerFactory implements ODataProducerFactory {
    private String persistenceUnitName = "persistence-with-ejb";
    private String namespace = "Example";
    private int maxResults = 50;
    @Override
    public ODataProducer create(final Properties arg0) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                persistenceUnitName);
        JPAProducer producer = new JPAProducer(
                emf,
                namespace,
                maxResults);
        return producer;
    }
}

Next we need to create the real odata service that uses our producer.

This means we need to configure a specific odata4j servlet and configure it.

Add the the specific servlet definitions at the end web-app sertion in web.xml file.

<!-- Odata4j producer -->
<!-- Servlet 1: Expose the OData service endpoint -->
<servlet>
          <servlet-name>OData</servlet-name>
          <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
          <init-param>
                    <param-name>javax.ws.rs.Application</param-name>
                    <param-value>org.odata4j.jersey.producer.resources.ODataApplication</param-value>
          </init-param>
          <init-param>
                    <param-name>odata4j.producerfactory</param-name>
                    <param-value>com.sap.cloud.sample.odata.ExampleProducerFactory</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
          <servlet-name>OData</servlet-name>
          <url-pattern>/odata/example.svc/*</url-pattern>
</servlet-mapping>
<!-- Servlet 2: Enable crossdomain access for browser clients -->
<servlet>
          <servlet-name>CrossDomain</servlet-name>
          <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
          <init-param>
                    <param-name>javax.ws.rs.Application</param-name>
                    <param-value>org.odata4j.producer.resources.RootApplication</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
          <servlet-name>CrossDomain</servlet-name>
          <url-pattern>/odata/*</url-pattern>
</servlet-mapping>

Next we can re-deploy and run the application in hanatrial.ondemand.com.

NB: remember to configure the target database in persistence.xml if you're using HANA Database: com.sap.persistence.platform.database.HDBPlatform

If everything is fine, we can reach the Odata Service here:

https://persistenceejb2p079297trial.hanatrial.ondemand.com/persistence-with-ejb/odata/example.svc/

Here the metadata:

https://persistenceejb2p079297trial.hanatrial.ondemand.com/persistence-with-ejb/odata/example.svc/$m...

Since we're using jersey, also the WADL is available:

https://persistenceejb2p079297trial.hanatrial.ondemand.com/persistence-with-ejb/odata/example.svc/ap...

References

Homepage of odata4j

How to host an odata4j producer with Apache Tomcat

Example of JPAProducer

Overview of odata4j maven module structure

7 Comments
Labels in this area