title: CRUD operation on OData service produced by Apache Olingo with MySQL, JPA and Tomcat web server
body: <p>I was following some <a href="https://blogs.sap.com/2014/02/05/odata-producer-using-apache-olingo-with-mysql-jpa-tomcat-web-server/" rel="nofollow noreferrer">example</a> 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.</p>
  
  <p>How can i perform <strong>operation like Create, Update and Delete</strong> based on above example in link.</p>
  
  <p><strong>Service.java</strong></p>
  
  <pre><code>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);          
          }       
      }   
  }
  </code></pre>
  
  <p><strong>Model.java</strong></p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p><strong>Persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
      &lt;persistence-unit name="ABC" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;class&gt;com.wuerth.model.Tilecollection&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/XXXXXXXXXXXXXXXX"/&gt;
              &lt;property name="javax.persistence.jdbc.user" value="XXXXX"/&gt;
              &lt;property name="javax.persistence.jdbc.password" value="XXXXX"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>Web.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
    &lt;display-name&gt;ABC&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
    &lt;servlet&gt;
      &lt;servlet-name&gt;ODataServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
        &lt;param-value&gt;org.apache.olingo.odata2.core.rest.app.ODataApplication&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;init-param&gt;
        &lt;param-name&gt;org.apache.olingo.odata2.service.factory&lt;/param-name&gt;
        &lt;param-value&gt;com.wuerth.main.TileCollectionListServiceFactory&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ODataServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/odata/v1/ABC_SRV/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
    &lt;!-- CORS To allow access OData service by JavaScript in other domain  --&gt;
    &lt;filter&gt;
        &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;org.apache.catalina.filters.CorsFilter&lt;/filter-class&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
        &lt;filter-name&gt;CorsFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/pONJJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pONJJ.png" alt="LIB Folder"></a></p>
  
  <p><strong>Index.html</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
   &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset="utf8"&gt;
          &lt;title&gt;OData&lt;/title&gt;
      &lt;/head&gt;## Heading ##
      &lt;body&gt;
          &lt;h1&gt;OData&lt;/h1&gt;&lt;br&gt;&lt;br&gt;
          &lt;a href="/ABC/odata/v1/ABC_SRV/"&gt;Service Document&lt;/a&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Please guide me with some good examples and concept.</p>
  
  <p>Thanking you in advance.</p>