OData producer using Apache Olingo with MySQL, JPA & Tomcat web server
Exposing MySQL database or any database as OData service endpoint using JPA (JAVA Persistence API) and Apache Olingo OData library on Apache Tomcat web server. The JPA Model can be exposed in the form of OData service endpoint, which allows the data to be accessed via http REST based protocol.
For more details on OData, please refer http://www.odata.org/
(I had a scenario, UI5 Application should run on SAP NetWeaver Gateway and as well in other OData source).
Developing of the OData endpoint using JPA Model and deployment of the OData service in Apache Tomcat in 7 steps.
Table of Contents
- Prerequisite
- 1. Create a dynamic web project in Eclipse
- 2. Create a MySQL connection to be exposed as OData service endpoint
- 3. Add the dependent libraries to the Project
- 4. Create a JPA Model from MySQL database connection
- 5. Expose your JPA Model as OData service using Apache Olingo
- 6. Testing OData service endpoint
- 7. OData service in CORS
Prerequisite
- MySQL Server 5.x
- Apache Tomcat 7.x
- Eclipse Kepler / Juno preferably J2EE edition
- Dependent libraries refer topic 3
1. Create a dynamic web project in Eclipse
- Launch Eclipse from extracted folder by double click eclipse.exe to open it. Eclipse will ask you for workspace location. Specify a location and click ok
- To create a Dynamic Web Project, File -> New -> Dynamic Web Project as shown
- Enter the project name for example “emplist-web”
- In Target runtime Click on “New Runtime…” button to define Apache Tomcat v7.0 as HTTP server to expose our JPA Model as OData service endpoint. Provide your Apache Tomcat Server folder in Tomcat installation directory.
- Click on “Modify…” button in the above window and in opened window, select the “JPA” check box and click on “OK” button
- In the next window, choose Platform “EclipseLink 2.5.x”, JPA implementation Type: “Disable Library Configuration”, radio button “Annotated classes must be listed in persistence.xml” and click on the “Next” button
- In the “Java – src” window, click on the “Next” button
- In the “Web Module” window, check “Generate web.xml deployment descriptor” and click on the “Finish” button. Now project has been created in the project explorer window
2. Create a MySQL connection to be exposed as OData service endpoint
- To create a MySQL Database connection, in the menu, Windows -> Show View -> Data Source Explorer
- Go to “Data Source Explorer” tab, Right click on “Database Connection” node and click on “New…” menu item
- In the Connection Profile window, select MySQL and click on “Next” button
- Configure the MySQL JDBC Driver i.e. add latest driver by removing non existent old driver.
- Specify the name for Database field and URL field end
- Enter the Username, Password and click “Test Connection” button ensure DB connection Ping succeed.
- In the “Data Source Explorer”, now you can see the MySQL connection and expand to see DB objects
3. Add the dependent libraries to the Project
Download and copy the required libraries to “<your eclipse project>/WebContent/WEB-INF/lib” folder.
- JPA: Add EclipseLink and JPA Persistence JAR files to “Lib” folder
- eclipselink.jar
- javax.persistence_2.1.0.v201304241213.jar
- Database: Add MySQL connector java to “Lib” folder
- mysql-connector-java-5.1.28-bin.jar
- OData: Add Apache Olingo OData libraries JAR files to “Lib” folder
- olingo-odata2-core-incubating-1.0.0.jar
- olingo-odata2-api-incubating-1.0.0.jar
- olingo-odata2-api-annotation-incubating-1.0.0.jar
- olingo-odata2-jpa-processor-core-incubating-1.0.0.jar
- olingo-odata2-jpa-processor-api-incubating-1.0.0.jar
- javax.ws.rs-javax.ws.rs-api-2.0-m10.jar
- Service: Other libraries required (main: http://central.maven.org/maven2/org/apache/cxf/)
- cxf-api-2.7.5.jar (../cxf-api/2.7.5/cxf-api-2.7.5.jar)
- cxf-rt-bindings-xml-2.7.5.jar (../cxf-rt-bindings-xml/2.7.5/cxf-rt-bindings-xml-2.7.5.jar)
- cxf-rt-core-2.7.5.jar (../cxf-rt-core/2.7.5/cxf-rt-core-2.7.5.jar
- cxf-rt-frontend-jaxrs-2.7.5.jar (../cxf-rt-frontend-jaxrs/2.7.5/cxf-rt-frontend-jaxrs-2.7.5.jar)
- cxf-rt-transports-http-2.7.5.jar (../cxf-rt-transports-http/2.7.5/cxf-rt-transports-http-2.7.5.jar)
- Links: downloading for libraries
- JPA: http://www.eclipse.org/eclipselink/downloads/
- MySQL DB driver: http://dev.mysql.com/downloads/connector/j/
- OData: http://www.apache.org/dyn/closer.cgi/incubator/olingo/odata2/rel-1.0.0/olingo-odata2-dist-jpa-incubating-1.0.0-jpa.zip
- Service: There itself
- After copying libraries to “/WebContent/WEB-INF/lib” folder, go to menu “Project” –> “Clean” and select your project by choosing “Clean projects selected below” and click on “OK” button.
- After creating JPA entities from table, Eclipse shows error then rightclick on “WebContent/WEB-INF/lib” folder and choose “Validate” menu item.
4. Create a JPA Model from MySQL database connection
- To create JPA Model, go to File –> New –> Other –> JPA Entities from Tables and click on next button
- Select MySQL connection and click connection button and select the schema where tables are available. Choose tables to be exposed as OData service and check “List generated classes in persistence.xml” and next window is “Table Associations”, here just click “Next” button.
- In Next window, check “Always generate optional JPA annotations and DDL parameters” and package can be configured here (com.<your company>.<project name>.model) and click on “Next” button.
- Next window click on “Finish” button.
- Now in “src” folder, Java classes are created as JPA Model entities.
- Double click on “persistence.xml” and select the tab “Connection”
- Transaction type: choose “Resource local” and EclipseLink connection pool area click on link “Populate from connection…” and choose Local MySQL and will populate the connection fields.
- Click source or select “persistence.xml” file from your project explorer will contain as follows
<?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=“emplist-web” transaction-type=“RESOURCE_LOCAL”> <class>model.Employee</class> <class>model.Group</class> <properties> <property name=“javax.persistence.jdbc.url” value=“jdbc:mysql://localhost:3306/test”/> <property name=“javax.persistence.jdbc.user” value=“root”/> <property name=“javax.persistence.jdbc.password” value=“King1234”/> <property name=“javax.persistence.jdbc.driver” value=“com.mysql.jdbc.Driver”/> </properties> </persistence-unit> </persistence> |
After creating JPA entities from table, Eclipse shows error then right click on “WebContent/WEB-INF/lib” folder and choose “Validate” menu item.
5. Expose your JPA Model as OData service using Apache Olingo
To Expose the JPA Model from MySQL as OData service, we use Apache Olingo library. For more information: http://olingo.incubator.apache.org/
Using Olingo, exposing JPA Model as OData service simple and it consists of three steps
- Extend the “ODataJPAServiceFactory” class by (EmployeeListServiceFactory.java)
- Creating a new class file “EmployeeListServiceFactory.java” in package “main” with following code (copy paste).
package main; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; 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 EmployeeListServiceFactory extends ODataJPAServiceFactory { private static final String PERSISTENCE_UNIT_NAME = “emplist-web”; @Override public ODataJPAContext initializeODataJPAContext() throws ODataJPARuntimeException { ODataJPAContext oDatJPAContext = this.getODataJPAContext(); try { EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); oDatJPAContext.setEntityManagerFactory(emf); oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
return oDatJPAContext; } catch (Exception e) { throw new RuntimeException(e); } } } |
- Add necessary tags in “web.xml” file
- Copy the contents in <servlet> and <servlet-mapping> tags
<?xml version=“1.0” encoding=“UTF-8”?> <web-app xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance“ xmlns=“http://java.sun.com/xml/ns/javaee“ xsi:schemaLocation=“http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“ id=“WebApp_ID” version=“3.0”> <display-name>emplist-web</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>main.EmployeeListServiceFactory</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ODataServlet</servlet-name> <url-pattern>/emplist.svc/*</url-pattern> </servlet-mapping> </web-app> |
- Add “index.html” to “WebContent” (Right click -> New HTML file) folder with following content
<!DOCTYPE html> <html> <head> <meta charset=“utf8”> <title>Employee List OData</title> </head> <body> <h1>Employee List OData</h1><br><br> <a href=“/emplist-web/emplist.svc/”>Service Document</a> </body> </html> |
6. Testing OData service endpoint
- Right click on your project in “Project Explorer”, select menu items “Run As” -> “Run on Server”.
- In the window, choose “Tomcat v7.0 Server at localhost” and click on “Finish” button
- After successfully web application deployed, you will get the below window (internal browser) or configured browser. In browser, “index.html” is shown and by clicking the link “Service Document”. Browser will show your OData Service document (http://localhost:8080/emplist-web/emplist.svc/). Better use Google Chrome browser. To access OData Metadata Service document add $metadata in the link (http://localhost:8080/emplist-web/emplist.svc/$metadata)
7. OData service in CORS
- Cross-origin resource sharing (CORS) is a mechanism that allows JavaScript on a web page to make XMLHttpRequests (AJAX calls) to another domain, not the domain the JavaScript originated from. Such “cross-domain” requests would otherwise be forbidden by web browsers, per the same origin security policy.
- UI5 application developed in other domain, requires access to your OData service forbidden by browsers.
- Allow access to OData service in other domain (CORS) then configure below tags in “/WebContent/ WEB-INF/web.xml” file in your project or globally for all web application then update in Apache Tomcat Server folder “/conf/web.xml”.
<!– 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> |
SAP Blog: Developing the OData Service | SCN
This blog helped me to find this solution and it is inspired me to write my own blog.
Good tutorial. please look into the below link, which has the detailed and step by step process of OData and sapui5 with HANA. The steps which was followed in this tutorial is already available in the below document.
SAP HANA Cloud Application Development Scenario End-to-End Tutorial
Great to see that Apache Olingo is used and thanks for sharing this blog. To get more insights to Apache Olingo please also have a look into the post OData - Insights to Apache Olingo Incubator
Thanks for your comment... 🙂 and Apache Olingo library maturing quicker by seeing your post.
Hi Satheekumar,
Nice blog. I am trying to implement it. but I get an error "Servlet ODataServlet is not available" after clicking on the service document. I raise quarry. Can you please guide me how to resolve the error???
Thanks
Himadri
Hi Himadri,
Please follow steps in section "3. Add the dependent libraries to the Project"
Hi Satheekumar,
Great post.
I have tried to implement an example with a simple Northwind database. At first sight all is working fine, but looking into the details there are a few issues:
Any suggestion on how these issues could be resolved?
Thanks,
Rudy
Rudy,
I have noticed that you must be careful to define the JPA relationship in both objects for Olingo to recognize it and have it appear in your metamodel. For example, a @ManyToOne relation in object A referencing object B must have a corresponding @OneToMany relation declared in B. I know odata4j was more permissive in its declaration of these relations.
I don't know if this will be changing in a future release, but that's how it works in Olingo 1.2.0.
I'm working on a more detailed article about my early exploration of Olingo. Hopefully I can publish it prior to SAPPHIRE.
Riley
Riley,
Many thanks for your reply. This is helpful.
I have tried the exact same scenario with Ollingo 1.2.0 (rather than 1.0.0 as described in the article) and found an improvement for the handling of relationships between entities. The relationships are now appearing in the service metadata and looks like they are correct. Looks like my first point has been resolved in this release.
On my second point, there seems to be no improvement. Certain datatypes are not handled correctly in the generated code and cause errors at execution time. I managed to correct most of the issues manually in the code, but would hope a future stable Olingo release would handle all possible combinations of data types.
I also found the new release to be introducing new issues, namely in the handling of Oracle as a DB layer under neath. So definitely more work to be done in order to make the Olingo libraries ready for use.
Best regards,
Rudy
Hi Rudy,
I have reported several issues to the Olingo issue tracking database.
I did notice a problem with the Date data type in my JPA model -- I was unable to POST or PUT fields with that Java type. In that case, I was able to resolve the issue by switching to java.util.Calendar. I'd be curious which other data type(s) you are experiencing problems with.
I'm not using Oracle, so I can't speak to issues there.
Riley
Old post but for future readers: thats quite accurate, I had the same problem in which relationships would not be shown and updating all OLingo libraries  to 2.0.8  instead of 1.0.0 solved the problem.
Thanks for sharing!
Hello Riley,
the problems I have experienced with mysql are with the "boolean" and "timestamp" data types. The boolean was converted to byte in the code, which did not work, and had to change it back to boolean. The MySQL timestamp was converted to a timestamp in java, but that did not work either, so had to convert it to Date.
The Oracle issue is still somewhat mysterious as I don't get a clear error message anywhere. Might be related to restrictions of the Oracle XE instance I am running to test, so will need to do further testing to confirm.
Best regards,
Rudy
Hello Riley, Hello Rudy,
It is nice to see you both using the library. I would like to tell that Olingo JPA Processor is a library that transforms the JPA models based on the JPA 2.0 spec into OData 2.0 spec.
From the description of the above issues, it seems the problem could lie with the JPA Providers. Some JPA providers handle data types in different way based on underlying DB platforms. Hence you might see some discrepancy. Olingo JPA processor library handles almost all data types that are mentioned in JPA 2.0 specification.
It would be nice if you can raise a issue in https://issues.apache.org/jira/browse/OLINGO so that we can improve the library.
Regards
Chandan
As suggested, I have raised issue OLINGO-329 for this. Thanks for the suggestion and hope it gets resolved in next official release.
Thanks,
Rudy
Thank you for providing this tutorial. I was able to follow it though easily until I came to step 5 -
I searched through my Eclipse project but could not find this file. I also followed the link - but it was just to the front page of the olingo project.
Could you elaborate on this step a bit to point me in the right direction?
Thanks,
Lester.
Hello Lester,
You need to create the new Java Class file and in which you need to extend/subclass the "ODataServiceFactory" class of Apache Olingo OData library. (Create the file and copy paste sample code)
Regards,
Sathees.
Hi Sathees,
I tried this using eclipse/Tomcat and seems to be working fine but when I tried the same scenario using NWDS(7.4)/SAP CE(7.4) Java Server, am getting HTTP 500 Internal Server Error as mentioned below.
Requesting help on the below.
Hi Satheeskumar Palaniappan,
I have done all the required things you have mentioned in the blog and I have cross checked with all your comments mentioned above,still I am getting the error as shown below.
Hi Keshav,
Try with this link "localhost:8080/emplist-web/index.html" and it opens in tomcat server then fine. It opens issue might be with eclipse proj or not opens issue with tomcat web server.
Or create web archive "war" file from eclipse project and keep this file in Tomcat's web apps folder and try it.
hello Sir,
Please look at this :
http://scn.sap.com/thread/3940187
Hi Sathees.!
I have gone though this blog and its very helpful too. I need more help from you.
I am getting the following result but i want output in a structured manner (perfect table) fetching data from MySQL.
Hello Satiskumar,
I have successfully tested this procedure (with a few issues) also with the newly release Olingo V2 2.0.0. libraries.
I do have a question about the implementation of the Odata V2 protocol as described here. The data is exposed successfully, but I am getting issues implementing basic data navigation directives like $filter. I was expecting the exposed service to allow the usage of these directives, but can not get them to work. Anything special needs to be done to activate that feature?
Best regards,
Rudy Gardelein
This example does not work, if you are not using Maven dependencies to gather all the required libraries. Then all you get is 404 response on web services calls.
One can mix this Olingo 2 tutorial Apache Olingo Library with this SAP tutorial to get it all working.
Actually, you just need to generate that Maven based project and to copy all the downloaded libraries into this project, and it will work.
Can anybody help with switching off caching of data?
If you update the data, you are still getting previous versions in the response.
You can search with filter for data records which were updated, and to get them in the response.
The problem is, I am getting cached data, even if I filtrated correct data records.
How to switch off data caching? (In some cases its needed)
Can you provide me with the hints about by what principles caching works in this solution and how to perform its configuration?
Sandjis,
Probably the easiest way to accomplish that is to add:
<shared-cache-mode>NONE</shared-cache-mode>
to your persistence.xml file.
Thank you! It helps.
In my case, I put in the model files this:
@Entity
@Cacheable(false)
However, now I will have to create some URL which must be called to inform web services app server that there are database contents changes and cache must be invalidated. Because the cache is not that bad thing in the project 🙂
I followed all the steps, mentioned here but I am getting the following error when I click on "Service Document" link.
type Exception report
message Servlet.init() for servlet ODataServlet threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
root cause
Same manner i followed but i got this following error
<error><code/><message xml:lang="en">java.lang.SecurityException: class "javax.persistence.PersistenceUtil"'s signer information does not match signer information of other classes in the same package</message></error>
can U please help me what was the problem
<error><code/><message xml:lang="en">java.lang.SecurityException: class "javax.persistence.PersistenceUtil"'s signer information does not match signer information of other classes in the same package</message></error>
Ramesh,
It would help if you could provide some details about your Java runtime environment and the Servlet/JEE container you are running the application in .. what version of Java are you using, what application container/version and what package/version of Java persistence?
jdk1.7.0_60
tomcat 7
i am using the follwoing version of jars and environment
jdk1.7.0_60
tomcat 7.0
Eclipse Juno Service Release 2
Build id: 20130225-0426
i am getting the following error
<error><code/><message xml:lang="en">java.lang.SecurityException: class "javax.persistence.PersistenceUtil"'s signer information does not match signer information of other classes in the same package</message></error>
Hello Ramesh,
I am facing the same issue. Could you please tell me how did you resolve the issue?
Thanks,
Ashwitha
Hi All,
I have gone though this blog and its very helpful too. I need more help from you.
I am getting the following result but i want output in a structured manner (perfect table) fetching data from MySQL.
Hi,
Thanks for the step by step tutorial.
I am getting below error
Exception during error handling occured!
There is no other detail on the exception
Could somebody please tell me what i am missing here
Thanks & Regards
Divya
Hi Divya,
I'm getting the exact same exception. I've absolutly no clue what is going wrong. Did you find a solution?
Regards
Holger
Hi Holger,
Just execute your sevice with query option odata-debug=json. This will give additional information with stack trace.
Thanks
Kind Regards
Chandan
Hi Chandan,
I've tried it, but unfortunately I don't receive any further information. Only the exception "Exception during error handling occured!" remains. Do you have any further tips?
Thanks in advance
Holger
Hi Sateesh,
I followed the above steps and successfully implemented using tomcat and mysql,
I exported the application as WAR file and try to deploy the application on WebSphere Application Server v8.5.
I am able to deploy the WAR file successfully but while invoking am unable to see the results. Will the same work with WAS server or does it need modification or config changes.???
Please do the needful...!!!
Thanks,
Praneeth
Hi,
I tried the above steps, while testing Odata service endpoint am getting the below error, please anyone help me to come out of it
Thanks in advance
-Mahendran
Hi Mahendran,
Which version of Olingo are you using? Secondly can you post your JPA entity structure for deeper analysis
Regards
Chandan
Hi,
@Chandan : For the above issue I've changed the data type in table level... now it is working fine for me and am able to list the data .....
from the data format I got another issue .....
I have no idea how to write filter for this service ....
while using filter am getting below error,
http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas?$filter=Name1 eq 'mahie'
HTTP Status 500 -
FYI...
am sharing the data format which I am getting from that Service,
http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas()/?$format=json
Output:
{"d":{"results":[{"__metadata":{"id":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('1')","uri":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('1')","type":"Customer_Reg.CustGeneralData"},"Aenam":null,"Anred":"2","Cin":null,"City1":"delhi","City2":"98","Ernam":null,"Ersda":null,"Kunnr":"1","Laeda":null,"Land1":null,"Mcod1":"3","Name1":"mahie","Name2":"5","Panno":null,"PoBox":"6","Pstlz":"9","Regio":null,"Smtp_addr":"mahi@gmail.vom","StrSuppl1":"7","StrSuppl2":"7","Stras":"6","TelNumber":"9738285552","Telf1":null,"Telfx":null,"Telx1":null,"Tin":null},
{"__metadata":{"id":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('2')","uri":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('2')","type":"Customer_Reg.CustGeneralData"},"Aenam":null,"Anred":"7","Cin":null,"City1":"bangalore","City2":null,"Ernam":null,"Ersda":null,"Kunnr":"2","Laeda":null,"Land1":null,"Mcod1":"7","Name1":"itenz","Name2":"8","Panno":null,"PoBox":"09","Pstlz":null,"Regio":null,"Smtp_addr":"test@ted.com","StrSuppl1":"0","StrSuppl2":"0","Stras":"9","TelNumber":"0989745984","Telf1":null,"Telfx":null,"Telx1":null,"Tin":null},
{"__metadata":{"id":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('6')","uri":"http://localhost:8080/Customer_Reg/custlist.svc/CustGeneralDatas('6')","type":"Customer_Reg.CustGeneralData"},"Aenam":null,"Anred":"tf","Cin":null,"City1":"test","City2":null,"Ernam":null,"Ersda":null,"Kunnr":"6","Laeda":null,"Land1":null,"Mcod1":"test","Name1":"test","Name2":"test","Panno":null,"PoBox":"test","Pstlz":null,"Regio":null,"Smtp_addr":"test","StrSuppl1":"test","StrSuppl2":"test","Stras":"test","TelNumber":"test","Telf1":null,"Telfx":null,"Telx1":null,"Tin":null}]}}
for Ex.
here i want to list the record based on some conditions like where Name1 = "test"
Pls help me on this ....
Regards,
Mahendran
Hi
Great post... I can read different data but I notice I run the format=json and still the data is displayed as XML e.g.
http://localhost:8080/emplist-web/emplist.svc/Citys(999)?format=json
The other thing is that I don't know if is on the scope of the article. But I tried to do a POST with no success. Can I Create, Update or Delete? or only READ.. If so.... can you give me some pointer on the best approach to tu do this.??
thanks
Henry
I think that the correct syntax is
http://localhost:8080/emplist-web/emplist.svc/Citys(999)?$format=json
I'm also facing problems on PUT, DELETE or PATCH Odata methods.
Did you find any solution?
thanks
Hi Simone,
Have a look at this recent article I wrote (link). The project source code is on Github and it can be built for Tomcat using Maven.
You will find modifications to the web.xml to allow for POST, PUT, DELETE, etc. to work properly with Olingo 2.0.
This article also shows how to configure an Olingo project to support delta tokens.
Hope that helps,
Riley
Thanks for the input!!
After reading your blog (and edited the xml) I continued having problems on some methods.
So I studied CorseFilter documents and blogs then i doubted on Chrome and RESTfull extension used for testing my Odata service.
I've created a sample SAPUI5 App with PUT POST and DELETE methods and everything worked like a charm.
Thank you!
Hi Experts,
I am facing one problem while running the service. Please help me.
thanks,
Prasad
Hello Prasad,
Check whether you have provided the <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> in your persistence.xml.
Check whether you have placed the persistence.xml in src/main/resouces/META-INF folder.
Check whether you have all required jars installed.
If you are running in OSGI env then you need to have the Meta-Persistence:META-INF/persistence.xml value in Manifest.mf
Regards
Chandan
hi Sir , I am also getting persistence exception while executing this program. please give me solution for this error . i have googled but still i didnt get any solution .
Great tutorial, it made me up to speed with Olingo Library on ODATA with JPA entities.
Question: Let say I have several tables with 1 to Many relationship, how does $expand works?.. I have tried but it doesnt work.
Can you point me into the right direction oh how it works?
Thanks
Hi,
$expand works when I restart the web server (tomcat). Again I add some data with POST method then try with $expand not shows recent data.
Experts throw some light on this issue.
Sathees
Switch off the cache in persistence.xml file & try
Riley Rainey Oct 15, 2014 5:11 PM (in response to Sandijs Aploks)
Sandjis,
Probably the easiest way to accomplish that is to add:
<shared-cache-mode>NONE</shared-cache-mode>
to your persistence.xml file.
Hi experts,
I tried the above steps, I am facing one problem while running the service. Please help me.
Thanks in advance
sowmya
Hi,
I'm facing issue during filter option please can you check this issue
java.lang.RuntimeException: org.apache.cxf.interceptor.Fault: Could not initialize class org.apache.olingo.odata2.core.uri.expression.FilterParserImpl
org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:116)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:331)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:239)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:223)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:203)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:243)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:168)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:219)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Hi,
Anyone pls help me... While using table with more than one key filed(composite key), the ODATA service throwing the following exception,
http://localhost:8080/sale_link/sale.svc/Attachments
Error:
and how to achieve bulk POST option in this service?
Thanks in advance.
-Mahendran
Hi Satheeskumar Palaniappan
Thank you for your blog. I have been looking for it for quite a while.
By the way, do you know how to produce OData without using JPA and Eclipselink?
I mean is it possible to do that by just using JDBC?
Moreover, can I produce OData for a JAVA method paramters instead of database fields?
Hi Ming Yu,
Yes it is possible refer the links: www.odata.org/libraries/#java
1. Apache Olingo
2. OData4j
Your specific need in JAVA you could use Apache Olingo / OData4j without JPA
1. Apache Olingo: http://olingo.apache.org/doc/odata4/index.html
2. OData4j: https://code.google.com/p/odata4j/OoD (Sec: Producers - Getting Started)
Hi Satheeskumar Palaniappan,
In SAP gateway, we can provide function module parameter as OData service.
I am wondering if we can do the same thing for JAVA method.
BTW, I didn't see any select in you example so I want to know if we can do that.
Hi Ming Yu,
1. BAPI/RFC wizard automatically creates required EntityType, Set and required methods.
no direct comparison with JAVA method to do I guess. Using JPA, database table simply exposed as ODATA.
2. JPA and Eclipselink takes care of connecting database table and fetching the data. All operations are automatic. (CRUDQ, Metadata,...)
3. Code based manual implementation for everything refer below link and you need to do lot.
https://olingo.apache.org/doc/odata4/tutorials/read/tutorial_read.htmlkind
In JAVA, no SEGW like builder to make things simple and JPA does that duty with some extent.
There seems to be one typo: In the "Expose your JPA Model as OData service using Apache Olingo section, the first bullet point (the first of three steps) says:
That should read ODataJPAServiceFactory.
The code you show below for the copy and paste does include the correct superclass. It's just in the description of the step when it's the wrong class.
Thanks Bruce, I updated typo error.
Hi Satheeskumar,
If possible please provide any link in sap hana odata with v4 version.
Thanks & Regards
Vivek
Hi,
it is very useful for beginners. i want to know how to read record from two tables based on condition ( like Select join query)
Thanks in advance,
Gnanamanikandan.R
Hi,
You can create view in DB Level and expose as a Entity.
Best Regards,
Sathees Kumar P.
Hi Sathees Kumar
Thanks for your reply i have one more doubt if i want use function how can we use it in this approach.if your providing guideline or sample it will very helpful for us.
Thanks in advance,
Gnanamanikandan.R
Hi.,
I am getting the below error while accessing odata service from another port of Tomcat service
XMLHttpRequest cannot load http://localhost:8081/sale_link/sale.svc/$metadata. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 403.
In my case.,I am running UI app from localhost:8080 and I deployed oData service in localhost:8081.,
I added the below code in both places Tomcat conf/web.xml & web-inf/web.xml
<!-- 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>
Anything I missed here? pls suggest me ..
-Mahendran
Hello Experts,
I followed all the steps and I am also getting the Odata Service,
When I tried to read data from this service in SAPUI5 application its working absolutely fine but its giving error in posting/updating data .
Please help.
I m getting following on console:
Hello Vinayak Khosla ,
Please try with Json type you can post and update your data to DB,
I will post a Example code below try this.
onPressSubmit : function()
{
var requestObj = {
requestUri : '',
method : '',
headers : {
"X-Requested-With" : "XMLHttpRequest",
"Content-Type" : "application/json",
"DataServiceVersion" : "2.0",
"MaxDataServiceVersion" : "2.0",
"Accept" : "application/json",
}
};
var newData = {"__metadata":{"id":"http/localhost:8080/server_link/citylist.svc/Citys("+key+")" ,,
"uri":"http/localhost:8080/server_link/citylist.svc/Citys("+key+")" ,
"type":"server_link.City"},
"Field1":Data1,
"Field2":Data2,
"Field3":Data3,
}
var url = "http/localhost:8080/server_link/citylist.svc/Citys"
var method = "POST";
requestObj.requestUri = url;
requestObj.method = method;
requestObj.data = newData;
OData.request(requestObj, function() {
alert("Success")
});
});
},
Please help! I have followed the tutorial properly. th eproblem i am facing is like
metadata is coming properly but the contents of the table are not coming
Metadata : http://localhost:8089/emplist-web/emplist.svc/$metadata
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="emplist-web">
<EntityType Name="EmpDb">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Dept" Type="Edm.String" Nullable="true" MaxLength="20"/>
<Property Name="FistName" Type="Edm.String" Nullable="true" MaxLength="20"/>
<Property Name="Id" Type="Edm.Int32" Nullable="false"/>
<Property Name="LastName" Type="Edm.String" Nullable="true" MaxLength="20"/>
</EntityType>
<EntityContainer Name="emplist-webContainer" m:IsDefaultEntityContainer="true">
<EntitySet Name="EmpDbs" EntityType="emplist-web.EmpDb"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Table Contents : http://localhost:8089/emplist-web/emplist.svc/
<service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom" xml:base="http://localhost:8089/emplist-web/emplist.svc/">
</collection>
</workspace>
</service>
Hello Binoy Subhakumar ,
Check this http://localhost:8089/emplist-web/emplist.svc/EmpDbs .
Thanks! It is working 🙂
I created a maven project due to which some changes I had to make in the steps mentioned in this tutorial.
The new MySQL Odata connector with detailed logging and high security - Skyvia Connect. It easily and with no coding exposes MySQL database as Odata endpoint.
Still a really useful tutorial! You should add commons-codec-1.6.jar (download from https://mvnrepository.com/artifact/commons-codec/commons-codec/1.6) to the dependencies - I got a class not found error when I tried a filter query until I added that jar.
Hi All,
I am able to create OData end-point and able to access the http://localhost:8080/emplist-web/emplist.svc/.
I want to provide user authentication to access this URL. So that only authorized user can access my data. Can anyone help me to provide user authentication in this project .
I am able to read the OData service using above explained steps. A very good document.
But when I was trying to perform CRUD operations, I was unable to do so using SAP UI5 Front end.
Can anyone post the code to Create, Upreplydate Delete using SAP UI5 app for the service created above.
Although it seems like whatever Odata service we created here do not support Create, Update Delete Operations.
Please anyone reply.
I am able to read the OData service using above explained steps. I was able to read the Data from SAP UI5 application but hard luck for Create, Update and Delete operations. A very good document though.
But when I was trying to perform CRUD operations, I was unable to do so using SAP UI5 Front end.
Can anyone post the code to Create, Upreplydate Delete using SAP UI5 app for the service created above.
Although it seems like whatever Odata service we created here do not support Create, Update Delete Operations.
Please anyone reply.