Introduction

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

Implementation:-

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=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”                xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“>

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

                 <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

                           <class>model.Employee</class>

                 <properties>

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

                </properties>

                </persistence-unit>

                </persistence>

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

     @Entity

     @Table(name=”Employee”)

     public class Employee {

            @Id

            @Column(name=”EmplID”)

            private String emplID;

            @Column(name=”FirstName”)

            private String firstName;

            @Column(name=”LastName”)

            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 java.net.URI;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

importorg.apache.olingo.odata2.jpa.processor.api.ODataJPAContext;

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

@Override

public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {

  ODataJPAContext oDataJPAContext = this.getODataJPAContext();

  try {

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

   uri.getQuery();

   EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

   oDataJPAContext.setEntityManagerFactory(emf);

   oDataJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);

    return oDataJPAContext;

  } catch (Exception e) {

   e.printStackTrace();

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

          http://localhost:8081/odata-sample/extdatasrc.svc/

/wp-content/uploads/2015/05/collection_703493.png

http://localhost:8081/odata-sample/extdatasrc.svc/Employees  

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

EmployeeList.png

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

This will fetch the reocrd with EmplID equals to 1.

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

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

http://localhost:8081/odata-sample/extdatasrc.svc/Employees?$select=FirstName

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

To report this post you need to login first.

3 Comments

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

  1. Priya Jha

    Hi ,

    I am new to OLingo.

    Dont we write :

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

    methods? Please guide.

    Regards,

    Priya

    (0) 
    1. MANUJ KUMAR Post author

      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.

      Thanks

      Manuj

      (0) 

Leave a Reply