Title: Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at . http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ## Creating a Web Application Project for Transforming JPA Models into OData Services In this section, information on how to create a web application (Maven) for transforming JPA Models into OData Services using OData JPA Processor Library is provided. The table gives the list of Maven dependencies you need to include in the POM.xml of your application. *Note*: The following dependencies are applicable for an application using EclipseLink as the JPA Provider and HSQLDB as the database. However, you are free to use any JPA provider (like Hibernate, OpenJPA and so on) and database of your choice. Group ID | Artifact ID | Version ---------| ----------- | ------- javax.servlet | servlet-api | 2.5 org.apache.cxf | cxf-rt-frontend-jaxrs | 2.7.5 org.slf4j | slf4j-log4j12 | 1.7.1 junit | junit | 3.8.1 org.apache.olingo | olingo.odata2.api | 1.0.0 org.apache.olingo | olingo.odata2.jpa.processor.api | 1.0.0 org.apache.olingo | olingo.odata2.jpa.processor.core | 1.0.0 org.apache.olingo | olingo.odata2.jpa.processor.ref | 1.0.0 org.apache.olingo | olingo.odata2.core | 1.0.0 org.eclipse.persistence | eclipselink | 2.3.1 org.eclipse.persistence | javax.persistence | 2.0.5 org.hsqldb | hsqldb | 2.2.8 Here is a [Sample JPA Model][1] ##### Create a Dynamic Web Application Project from Scratch: 1. In the command prompt, enter the maven command given here (change the DgroupId and DartifactId as per your requirement) mvn archetype:generate -DgroupId=com.sample.jpa -DartifactId=salesorderprocessing.app -DarchetypeArtifactId=maven-archetype-webapp Maven generates the file system structure for a web application project including a basic POM.xml. This step is completed by creating a Java source folder. 2. Create a folder by name 'java' in the path 'src/main/'. ##### Tailor POM.xml POM.xml should be modified for adding dependencies like OData Library (Java) and OData JPA Processor Library. Add a dependency to the project that contains JPA models. Open POM.xml and replace the existing content with the following: 4.0.0 org.apache.olingo olingo-odata2-parent 1.0.0 .. olingo.odata2.jpa.processor.ref.web war ${project.groupId}-${project.artifactId} javax.servlet servlet-api 2.5 provided org.apache.cxf cxf-rt-frontend-jaxrs 2.7.5 org.apache.olingo olingo-odata2-core ${project.version} org.apache.olingo olingo-odata2-api ${project.version} org.apache.olingo olingo-odata2-jpa-processor-api ${project.version} org.apache.olingo olingo-odata2-jpa-processor-core ${project.version} org.apache.olingo olingo-odata2-jpa-processor-ref ${project.version} org.slf4j slf4j-log4j12 1.7.1 junit junit 3.8.1 test org.eclipse.persistence eclipselink 2.3.1 org.eclipse.persistence javax.persistence 2.0.5 org.hsqldb hsqldb 2.2.8 olingo.odata2.jpa.processor.ref.web ##### Implement an OData Service The project is now ready to expose OData services. Service Factory provides a means for initializing Entity Data Model (EDM) Provider and OData JPA Processors. Following are the steps for implementing a Service Factory: 1. Add a new Java class by extending ODataJPAServiceFactory. 2. Declare persistence unit name as class variable. For example, private static final String PUNIT_NAME = "persistenceUnitName"; *Note*: The PUNIT_NAME refers to the persistence unit name maintained in the persistence.xml of JPA project. 3. Implement the abstract method `initializeODataJPAContext`. Here is the code snippet: ODataJPAContext oDataJPAContext = getODataJPAContext(); oDataJPAContext.setEntityManagerFactory(JPAEntityManagerFactory.getEntityManagerFactory(PUNIT_NAME)); oDataJPAContext.setPersistenceUnitName(PUNIT_NAME); ##### Configure the Web Application 1. Configure the web application as shown below by adding the following servlet configuration to web.xml. The Service factory which was implemented is configured in the web.xml of the ODataApplication as one of the init parameters. 2. Replace in the following XML with the class name you created in the previous step: - JPARefScenarioServlet org.apache.cxf.jaxrs.servlet.CXFNonSpringJaxrsServlet - javax.ws.rs.Application org.apache.olingo.odata2.core.rest.app.ODataApplication - org.apache.olingo.odata2.service.factory org.apache.olingo.odata2.jpa.processor.ref.web.JPAReferenceServiceFactory 1 - JPARefScenarioServlet /SalesOrderProcessing.svc/* After the implementation, test the web application using http://localhost:8080/olingo.odata2.jpa.processor.ref.web/SalesOrderProcessing.svc [1]: ../../../resources/Sample_JPA_Model.xml