CRUD operation on OData service produced by Apache Olingo with MySQL, JPA and Tomcat web server

3.1k Views Asked by At

I was following some example in which we can able to produce OData service using Apache Olingo with MySQL, JPA and Tomcat web server. This example is completely based on displaying data from MySQL database.

How can i perform operation like Create, Update and Delete based on above example in link.

Service.java

import org.apache.olingo.odata2.jpa.processor.ref.factory.JPAEntityManagerFactory;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAServiceFactory;
import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;

public class TileCollectionListServiceFactory extends ODataJPAServiceFactory {

    private static final String PERSISTENCE_UNIT_NAME = "ABC";

    @Override
    public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException {
        ODataJPAContext oDatJPAContext = this.getODataJPAContext();
        try {           
            oDatJPAContext.setEntityManagerFactory(JPAEntityManagerFactory.getEntityManagerFactory(PERSISTENCE_UNIT_NAME));
//          oDataJPAContext.setPersistenceUnitName(PUNIT_NAME);

//          EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
//          oDatJPAContext.setEntityManagerFactory(emf);
            oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);

            return oDatJPAContext;          
        } catch (Exception e) {         
            throw new RuntimeException(e);          
        }       
    }   
}

Model.java

import java.io.Serializable;
import javax.persistence.*;

@Entity
@NamedQuery(name="Tilecollection.findAll", query="SELECT t FROM Tilecollection t")
public class Tilecollection implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idtilecollection;

    private String icon;

    private String isAdmin;

    private String title;

    private String type;

    public Tilecollection() {
    }

    public int getIdtilecollection() {
        return this.idtilecollection;
    }

    public void setIdtilecollection(int idtilecollection) {
        this.idtilecollection = idtilecollection;
    }

    public String getIcon() {
        return this.icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getIsAdmin() {
        return this.isAdmin;
    }

    public void setIsAdmin(String isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="ABC" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.wuerth.model.Tilecollection</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/XXXXXXXXXXXXXXXX"/>
            <property name="javax.persistence.jdbc.user" value="XXXXX"/>
            <property name="javax.persistence.jdbc.password" value="XXXXX"/>
        </properties>
    </persistence-unit>
</persistence>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ABC</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>ODataServlet</servlet-name>
    <servlet-class>org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>org.apache.olingo.odata2.core.rest.app.ODataApplication</param-value>
    </init-param>
    <init-param>
      <param-name>org.apache.olingo.odata2.service.factory</param-name>
      <param-value>com.wuerth.main.TileCollectionListServiceFactory</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>ODataServlet</servlet-name>
    <url-pattern>/odata/v1/ABC_SRV/*</url-pattern>
  </servlet-mapping>

  <!-- CORS To allow access OData service by JavaScript in other domain  -->
  <filter>
      <filter-name>CorsFilter</filter-name>
      <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

LIB Folder

Index.html

<!DOCTYPE html>
 <html>
    <head>
        <meta charset="utf8">
        <title>OData</title>
    </head>## Heading ##
    <body>
        <h1>OData</h1><br><br>
        <a href="/ABC/odata/v1/ABC_SRV/">Service Document</a>
    </body>
</html>

Please guide me with some good examples and concept.

Thanking you in advance.

1

There are 1 best solutions below

0
On

You can refer to SCN blog about mysql and Olingo Odata2 based example. I found this blog helpful in developing my apps.