Skip to Content
Author's profile photo Former Member

Expose JPA Entity as Odata Service with Odata4j

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
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


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 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:

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;
    public ODataProducer create(final Properties arg0) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
        JPAProducer producer = new JPAProducer(
        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 2: Enable crossdomain access for browser clients -->

Next we can re-deploy and run the application in

NB: remember to configure the target database in persistence.xml if you’re using HANA Database:

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

Here the metadata:$metadata

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


Homepage of odata4j

How to host an odata4j producer with Apache Tomcat

Example of JPAProducer

Overview of odata4j maven module structure

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo VINCENZO TURCO

      Hi Francesco,

      great blog, thanks for sharing.

      I am currently investigating options for exposing OData services on the Cloud, and I found out another OData library

      A usage scenario is described here

      I would highly appreciate any opinion on this library and how it compares to the one you described in  the blog (easier/more powerful/...).

      I am also creating a SDN thread for this subject, any feedback is welcome.

      Thanks a lot


      Author's profile photo Jens Glander
      Jens Glander

      Hi Francesco,

      I started originally with the well known PersonsList GettingStarted application tutorial also with the odata4j approach like you did with your very nice blog.

      Later also the ESPM application scenarios, with a more complex JPA model serving a WebShop and Retailer application (see were developed and based initially on odata4j.

      Nevertheless it turned out that our ESPM team had several issues to get the more complex ESPM JPA model exposed as OData service when using odata4j.

      For that reason we based our SAP HCP example applications (PersonsList and ESPM) where JPA and OData connectivity is used, technically on the Olingo OData libraries.

      At least our developer experience was better when using Olingo OData with JPA and also we support this new open source project by own committers.

      Would be nice if you could also have a look at the Olingo approach (see e.g. todays updated PersonsList Application) and let us know about your further experience.   

      Best regards,


      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Hi Jens

      thank you for your comment.

      i've seen the olingo project, very interesting.

      It also seems that the project is more active than odata4j. For sure i'll use this for next odata implementation.

      I've used odata4j for a very stupid reason, it was the first hint in google search. Probably beacuse it's an older project.

      Which kind of issues had you using odata4j?

      Best Regards


      Author's profile photo Stephen Cherian
      Stephen Cherian

      Hi Francesco,

        To answer your question regarding what kind of issues we faced with OData4j

      • No support for function imports
      • immutable entity set names. Usually the name of an OData entity set is the plural word of the corresponding entity type's name. Unfortunately the JPAProducer included in odata4j just takes the JPA entity type name for the OData entity set name as well and doesn't provide any means to adapt it.
      • Non standard property names (lower/upper case) . Properties of JPA entity beans are usually named according to the standard lower camel case naming convention, whereas properties in an OData metadata service document usually follow the upper camel case naming convention.

      These could be overcome in Olingo. As mentioned by Jens you can refer to ESPM application scenarios with a more detailed implementation with JPA, Olingo for OData connectivity.

      Best Regards


      Author's profile photo Former Member
      Former Member


      To make it run on Java Web Tomcat 8 Server, I had to include in web_inf\lib

      core4j-0..jar, jersey_bundle-1.19.1.jar joda_time-1.19.jar odata4j-core odata4j-cxf  and odata4j-jersey.

      However I get an exception on hana cloud platfrom :

      2016 07 29 15:32:09#+00#ERROR#org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/ejb_jpa]##anonymous#http-bio-8041-exec-8#na#i022455sapdev#ejbjpa#web#i022455sapdev#StandardWrapper.Throwablejava.lang.RuntimeException: javax.naming.NameNotFoundException: Name [CDIExtension] is not bound in this Context. Unable to find [CDIExtension]. at com.sun.jersey.server.impl.cdi.CDIExtension.getInitializedExtension( at com.sun.jersey.server.impl.cdi.CDIComponentProviderFactory.( at com.sun.jersey.server.impl.cdi.CDIComponentProviderFactoryInitializer.initialize( at com.sun.jersey.spi.container.servlet.WebComponent.configure( at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.configure( at com.sun.jersey.spi.container.servlet.WebComponent.load( at com.sun.jersey.spi.container.servlet.WebComponent.init( at com.sun.jersey.spi.container.servlet.ServletContainer.init( at com.sun.jersey.spi.container.servlet.ServletContainer.init( at javax.servlet.GenericServlet.init( at org.apache.catalina.core.StandardWrapper.initServlet( at org.apache.catalina.core.StandardWrapper.loadServlet( at org.apache.catalina.core.StandardWrapper.allocate( at org.apache.catalina.core.StandardWrapperValve.invoke( at org.apache.catalina.core.StandardContextValve.invoke( at at org.apache.catalina.authenticator.AuthenticatorBase.invoke( at at org.apache.catalina.core.StandardHostValve.invoke( at org.apache.catalina.valves.AccessLogValve.invoke( at at at at at org.apache.catalina.valves.ErrorReportValve.invoke( at org.apache.catalina.core.StandardEngineValve.invoke( at org.apache.catalina.connector.CoyoteAdapter.service( at org.apache.coyote.http11.AbstractHttp11Processor.process( at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process( at$ at java.util.concurrent.ThreadPoolExecutor.runWorker( at java.util.concurrent.ThreadPoolExecutor$ at org.apache.tomcat.util.threads.TaskThread$ at Caused by: javax.naming.NameNotFoundException: Name [CDIExtension] is not bound in this Context. Unable to find [CDIExtension]. at org.apache.naming.NamingContext.lookup( at org.apache.naming.NamingContext.lookup( at com.sun.jersey.server.impl.cdi.CDIExtension.getInitializedExtension( ... 33 common frames omitted Any Idea ?

      Author's profile photo Former Member
      Former Member

      Hi Experts.,


                         I want to achieve deep entity creation using JPA. Anyone please guide me on the same?


      Author's profile photo Michael Appleby
      Michael Appleby

      Unless you are asking for clarification/correction of some part of the Document, please create a new Discussion marked as a Question.  The Comments section of a Blog (or Document) is not the right vehicle for asking questions as the results are not easily searchable.  Once your issue is solved, a Discussion with the solution (and marked with Correct Answer) makes the results visible to others experiencing a similar problem.  If a blog or document is related, put in a link.  Read the Getting Started documents (link at the top right) including the Rules of Engagement. 

      NOTE: Getting the link is easy enough for both the author and Blog.  Simply MouseOver the item, Right Click, and select Copy Shortcut.  Paste it into your Discussion.  You can also click on the url after pasting.  Click on the A to expand the options and select T (on the right) to Auto-Title the url.

      Thanks, Mike (Moderator)

      SAP Technology RIG