Skip to Content

Defining OData, Creating OData project using OLingo,JPA, Maven


First lets try to understand what OData is about:-

1.) Odata helps you in building and consuming restful webservices.

2.) It helps you focus only on business logic rather focusing on request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats and query options etc.that we have to while developing restful webservices.

3.) Additionally, OData provides facility for extension to fullfil any custom needs of your RESTful APIs.

4.) OData (Open Data Protocol) is an OASIS standard

So now we could say that OData is a Restful webservice with a standardized XML format.

Now to create OData server in java, apache has provided us a very useful library called OLingo.

OLingo library can be created with or without JPA, but when we create OData project without JPA following OData features can not be used:-

$filter :- It works as where condition in sql

$select :- It specifies the field for which the select is being fetched.

$top :- works as rownum in the query


Now lets have a look at creating the first Olingo project:-

1.)Create a maven project using following command:-

mvn archetype:generate -DgroupId=com.sf.example -DartifactId=odata-sample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2.)Create a pom.xml file as attached, this will download all the required of olingo to with OData2.

3.)Now create the persistence.xml as shown:-

          <?xml version=”1.0″ encoding=”UTF-8″?>

               <persistence version=”2.0″ xmlns=”” xmlns:xsi=””                xsi:schemaLocation=”“>

                 <persistence-unit name=”mysql” transaction-type=”RESOURCE_LOCAL”>




                                   <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/EmpManagement” />

                                    <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver” />

                                    <property name=”javax.persistence.jdbc.user” value=”root” />

                                    <property name=”javax.persistence.jdbc.password” value=”root” />




4.) Now create a table Employee in data base and create the entity class for that as shown:-



     public class Employee {



            private String emplID;


            private String firstName;


            private String lastName;

            public String getEmplID() {

                 return emplID;


            public void setEmplID(String emplID) {

                 this.emplID = emplID;


            public String getFirstName() {

                 return firstName;


            public void setFirstName(String firstName) {

                 this.firstName = firstName;


            public String getLastName() {

                 return lastName;


             public void setLastName(String lastName) {

                 this.lastName = lastName;



5.) Now lets create the most important file for our OData project as shown:-

package main;


import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;


import org.apache.olingo.odata2.jpa.processor.api.ODataJPAServiceFactory;

import org.apache.olingo.odata2.jpa.processor.api.exception.ODataJPARuntimeException;

public class PolicyServiceFactory extends ODataJPAServiceFactory {

private static final String PERSISTENCE_UNIT_NAME = “mysql”;


public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {

  ODataJPAContext oDataJPAContext = this.getODataJPAContext();

  try {

   URI uri = oDataJPAContext.getODataContext().getPathInfo().getRequestUri();


   EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);



    return oDataJPAContext;

  } catch (Exception e) {


    throw new RuntimeException(e);




6.) Now apply the following maven commands to compile and create the war file for the given project:-

          mvn clean

          mvn compile

          mvn install

7.) Now place the created war at webapps folder of tomcat to deploy our project on tomcat.

8.) Now once the project is successfully deployed use following url and see how easy it has become access the database from browser itself:-




This will fetch you all the records in Employee table as shown:-


http://localhost:8081/odata-sample/extdatasrc.svc/Employees?$filter=EmplD  eq  1

This will fetch the reocrd with EmplID equals to 1.


This will fetch the records with rownum as less then or equal to 1.


It should fetch all the reocrds but with specific column, for FirstName in this case

You must be Logged on to comment or reply to a post.
  • Hi ,

    I am new to OLingo.

    Dont we write :

    • getEntityType()
    • getEntitySet()
    • getEntityContainer()
    • getSchemas()
    • getEntityContainerInfo()

    methods? Please guide.



    • Hi Priya,

      Why do you want to write these, can you please explain as I don’t see need of writing these methods when OLingo is handling all these things for us.

      Had we been using some ORM separately then definitely we would have wrote these methods.

      But yes we can access EntityManager using following code:-

      EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

      EnetityManager em = emf.createEntityManager();

      By doing this you can access any of JPA methods.